codechecker.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\App;
  9. use OC\Hooks\BasicEmitter;
  10. use PhpParser\Lexer;
  11. use PhpParser\Node;
  12. use PhpParser\Node\Name;
  13. use PhpParser\NodeTraverser;
  14. use PhpParser\NodeVisitorAbstract;
  15. use PhpParser\Parser;
  16. use RecursiveCallbackFilterIterator;
  17. use RecursiveDirectoryIterator;
  18. use RecursiveIteratorIterator;
  19. use RegexIterator;
  20. use SplFileInfo;
  21. class CodeChecker extends BasicEmitter {
  22. const CLASS_EXTENDS_NOT_ALLOWED = 1000;
  23. const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001;
  24. const STATIC_CALL_NOT_ALLOWED = 1002;
  25. const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
  26. const CLASS_NEW_FETCH_NOT_ALLOWED = 1004;
  27. /** @var Parser */
  28. private $parser;
  29. /** @var string[] */
  30. private $blackListedClassNames;
  31. public function __construct() {
  32. $this->parser = new Parser(new Lexer);
  33. $this->blackListedClassNames = [
  34. // classes replaced by the public api
  35. 'OC_API',
  36. 'OC_App',
  37. 'OC_AppConfig',
  38. 'OC_Avatar',
  39. 'OC_BackgroundJob',
  40. 'OC_Config',
  41. 'OC_DB',
  42. 'OC_Files',
  43. 'OC_Helper',
  44. 'OC_Hook',
  45. 'OC_Image',
  46. 'OC_JSON',
  47. 'OC_L10N',
  48. 'OC_Log',
  49. 'OC_Mail',
  50. 'OC_Preferences',
  51. 'OC_Request',
  52. 'OC_Response',
  53. 'OC_Template',
  54. 'OC_User',
  55. 'OC_Util',
  56. ];
  57. }
  58. /**
  59. * @param string $appId
  60. * @return array
  61. */
  62. public function analyse($appId) {
  63. $appPath = \OC_App::getAppPath($appId);
  64. if ($appPath === false) {
  65. throw new \RuntimeException("No app with given id <$appId> known.");
  66. }
  67. return $this->analyseFolder($appPath);
  68. }
  69. /**
  70. * @param string $folder
  71. * @return array
  72. */
  73. public function analyseFolder($folder) {
  74. $errors = [];
  75. $excludes = array_map(function($item) use ($folder) {
  76. return $folder . '/' . $item;
  77. }, ['vendor', '3rdparty', '.git', 'l10n']);
  78. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  79. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  80. /** @var SplFileInfo $item */
  81. foreach($excludes as $exclude) {
  82. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  83. return false;
  84. }
  85. }
  86. return true;
  87. });
  88. $iterator = new RecursiveIteratorIterator($iterator);
  89. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  90. foreach ($iterator as $file) {
  91. /** @var SplFileInfo $file */
  92. $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]);
  93. $errors = array_merge($this->analyseFile($file), $errors);
  94. $this->emit('CodeChecker', 'analyseFileFinished', [$errors]);
  95. }
  96. return $errors;
  97. }
  98. /**
  99. * @param string $file
  100. * @return array
  101. */
  102. public function analyseFile($file) {
  103. $code = file_get_contents($file);
  104. $statements = $this->parser->parse($code);
  105. $visitor = new CodeCheckVisitor($this->blackListedClassNames);
  106. $traverser = new NodeTraverser;
  107. $traverser->addVisitor($visitor);
  108. $traverser->traverse($statements);
  109. return $visitor->errors;
  110. }
  111. }