CodeChecker.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\App\CodeChecker;
  25. use OC\Hooks\BasicEmitter;
  26. use PhpParser\Lexer;
  27. use PhpParser\NodeTraverser;
  28. use PhpParser\Parser;
  29. use RecursiveCallbackFilterIterator;
  30. use RecursiveDirectoryIterator;
  31. use RecursiveIteratorIterator;
  32. use RegexIterator;
  33. use SplFileInfo;
  34. class CodeChecker extends BasicEmitter {
  35. const CLASS_EXTENDS_NOT_ALLOWED = 1000;
  36. const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001;
  37. const STATIC_CALL_NOT_ALLOWED = 1002;
  38. const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
  39. const CLASS_NEW_NOT_ALLOWED = 1004;
  40. const OP_OPERATOR_USAGE_DISCOURAGED = 1005;
  41. const CLASS_USE_NOT_ALLOWED = 1006;
  42. const CLASS_METHOD_CALL_NOT_ALLOWED = 1007;
  43. /** @var Parser */
  44. private $parser;
  45. /** @var ICheck */
  46. protected $checkList;
  47. /** @var bool */
  48. protected $checkMigrationSchema;
  49. public function __construct(ICheck $checkList, $checkMigrationSchema) {
  50. $this->checkList = $checkList;
  51. $this->checkMigrationSchema = $checkMigrationSchema;
  52. $this->parser = new Parser(new Lexer);
  53. }
  54. /**
  55. * @param string $appId
  56. * @return array
  57. * @throws \RuntimeException if app with $appId is unknown
  58. */
  59. public function analyse(string $appId): array {
  60. $appPath = \OC_App::getAppPath($appId);
  61. if ($appPath === false) {
  62. throw new \RuntimeException("No app with given id <$appId> known.");
  63. }
  64. return $this->analyseFolder($appId, $appPath);
  65. }
  66. /**
  67. * @param string $appId
  68. * @param string $folder
  69. * @return array
  70. */
  71. public function analyseFolder(string $appId, string $folder): array {
  72. $errors = [];
  73. $excludedDirectories = ['vendor', '3rdparty', '.git', 'l10n', 'tests', 'test', 'build'];
  74. if ($appId === 'password_policy') {
  75. $excludedDirectories[] = 'lists';
  76. }
  77. $excludes = array_map(function($item) use ($folder) {
  78. return $folder . '/' . $item;
  79. }, $excludedDirectories);
  80. $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
  81. $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
  82. /** @var SplFileInfo $item */
  83. foreach($excludes as $exclude) {
  84. if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. });
  90. $iterator = new RecursiveIteratorIterator($iterator);
  91. $iterator = new RegexIterator($iterator, '/^.+\.php$/i');
  92. foreach ($iterator as $file) {
  93. /** @var SplFileInfo $file */
  94. $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]);
  95. $fileErrors = $this->analyseFile($file->__toString());
  96. $this->emit('CodeChecker', 'analyseFileFinished', [$file->getPathname(), $fileErrors]);
  97. $errors = array_merge($fileErrors, $errors);
  98. }
  99. return $errors;
  100. }
  101. /**
  102. * @param string $file
  103. * @return array
  104. */
  105. public function analyseFile(string $file): array {
  106. $code = file_get_contents($file);
  107. $statements = $this->parser->parse($code);
  108. $visitor = new NodeVisitor($this->checkList);
  109. $migrationVisitor = new MigrationSchemaChecker();
  110. $traverser = new NodeTraverser;
  111. $traverser->addVisitor($visitor);
  112. if ($this->checkMigrationSchema && preg_match('#^.+\\/Migration\\/Version[^\\/]{1,255}\\.php$#i', $file)) {
  113. $traverser->addVisitor($migrationVisitor);
  114. }
  115. $traverser->traverse($statements);
  116. return array_merge($visitor->errors, $migrationVisitor->errors);
  117. }
  118. }