You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.2 KiB

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