@@ -36,105 +36,105 @@ |
||
| 36 | 36 | |
| 37 | 37 | class CodeChecker extends BasicEmitter { |
| 38 | 38 | |
| 39 | - const CLASS_EXTENDS_NOT_ALLOWED = 1000; |
|
| 40 | - const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001; |
|
| 41 | - const STATIC_CALL_NOT_ALLOWED = 1002; |
|
| 42 | - const CLASS_CONST_FETCH_NOT_ALLOWED = 1003; |
|
| 43 | - const CLASS_NEW_NOT_ALLOWED = 1004; |
|
| 44 | - const OP_OPERATOR_USAGE_DISCOURAGED = 1005; |
|
| 45 | - const CLASS_USE_NOT_ALLOWED = 1006; |
|
| 46 | - const CLASS_METHOD_CALL_NOT_ALLOWED = 1007; |
|
| 47 | - |
|
| 48 | - /** @var Parser */ |
|
| 49 | - private $parser; |
|
| 50 | - |
|
| 51 | - /** @var ICheck */ |
|
| 52 | - protected $checkList; |
|
| 53 | - |
|
| 54 | - /** @var bool */ |
|
| 55 | - protected $checkMigrationSchema; |
|
| 56 | - |
|
| 57 | - public function __construct(ICheck $checkList, $checkMigrationSchema) { |
|
| 58 | - $this->checkList = $checkList; |
|
| 59 | - $this->checkMigrationSchema = $checkMigrationSchema; |
|
| 60 | - $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param string $appId |
|
| 65 | - * @return array |
|
| 66 | - * @throws \RuntimeException if app with $appId is unknown |
|
| 67 | - */ |
|
| 68 | - public function analyse(string $appId): array { |
|
| 69 | - $appPath = \OC_App::getAppPath($appId); |
|
| 70 | - if ($appPath === false) { |
|
| 71 | - throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return $this->analyseFolder($appId, $appPath); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $appId |
|
| 79 | - * @param string $folder |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public function analyseFolder(string $appId, string $folder): array { |
|
| 83 | - $errors = []; |
|
| 84 | - |
|
| 85 | - $excludedDirectories = ['vendor', '3rdparty', '.git', 'l10n', 'tests', 'test', 'build']; |
|
| 86 | - if ($appId === 'password_policy') { |
|
| 87 | - $excludedDirectories[] = 'lists'; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $excludes = array_map(function($item) use ($folder) { |
|
| 91 | - return $folder . '/' . $item; |
|
| 92 | - }, $excludedDirectories); |
|
| 93 | - |
|
| 94 | - $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS); |
|
| 95 | - $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($excludes){ |
|
| 96 | - /** @var SplFileInfo $item */ |
|
| 97 | - foreach($excludes as $exclude) { |
|
| 98 | - if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - return true; |
|
| 103 | - }); |
|
| 104 | - $iterator = new RecursiveIteratorIterator($iterator); |
|
| 105 | - $iterator = new RegexIterator($iterator, '/^.+\.php$/i'); |
|
| 106 | - |
|
| 107 | - foreach ($iterator as $file) { |
|
| 108 | - /** @var SplFileInfo $file */ |
|
| 109 | - $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]); |
|
| 110 | - $fileErrors = $this->analyseFile($file->__toString()); |
|
| 111 | - $this->emit('CodeChecker', 'analyseFileFinished', [$file->getPathname(), $fileErrors]); |
|
| 112 | - $errors = array_merge($fileErrors, $errors); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $errors; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @param string $file |
|
| 121 | - * @return array |
|
| 122 | - */ |
|
| 123 | - public function analyseFile(string $file): array { |
|
| 124 | - $code = file_get_contents($file); |
|
| 125 | - $statements = $this->parser->parse($code); |
|
| 126 | - |
|
| 127 | - $visitor = new NodeVisitor($this->checkList); |
|
| 128 | - $migrationVisitor = new MigrationSchemaChecker(); |
|
| 129 | - $traverser = new NodeTraverser; |
|
| 130 | - $traverser->addVisitor($visitor); |
|
| 131 | - |
|
| 132 | - if ($this->checkMigrationSchema && preg_match('#^.+\\/Migration\\/Version[^\\/]{1,255}\\.php$#i', $file)) { |
|
| 133 | - $traverser->addVisitor($migrationVisitor); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $traverser->traverse($statements); |
|
| 137 | - |
|
| 138 | - return array_merge($visitor->errors, $migrationVisitor->errors); |
|
| 139 | - } |
|
| 39 | + const CLASS_EXTENDS_NOT_ALLOWED = 1000; |
|
| 40 | + const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001; |
|
| 41 | + const STATIC_CALL_NOT_ALLOWED = 1002; |
|
| 42 | + const CLASS_CONST_FETCH_NOT_ALLOWED = 1003; |
|
| 43 | + const CLASS_NEW_NOT_ALLOWED = 1004; |
|
| 44 | + const OP_OPERATOR_USAGE_DISCOURAGED = 1005; |
|
| 45 | + const CLASS_USE_NOT_ALLOWED = 1006; |
|
| 46 | + const CLASS_METHOD_CALL_NOT_ALLOWED = 1007; |
|
| 47 | + |
|
| 48 | + /** @var Parser */ |
|
| 49 | + private $parser; |
|
| 50 | + |
|
| 51 | + /** @var ICheck */ |
|
| 52 | + protected $checkList; |
|
| 53 | + |
|
| 54 | + /** @var bool */ |
|
| 55 | + protected $checkMigrationSchema; |
|
| 56 | + |
|
| 57 | + public function __construct(ICheck $checkList, $checkMigrationSchema) { |
|
| 58 | + $this->checkList = $checkList; |
|
| 59 | + $this->checkMigrationSchema = $checkMigrationSchema; |
|
| 60 | + $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param string $appId |
|
| 65 | + * @return array |
|
| 66 | + * @throws \RuntimeException if app with $appId is unknown |
|
| 67 | + */ |
|
| 68 | + public function analyse(string $appId): array { |
|
| 69 | + $appPath = \OC_App::getAppPath($appId); |
|
| 70 | + if ($appPath === false) { |
|
| 71 | + throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return $this->analyseFolder($appId, $appPath); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $appId |
|
| 79 | + * @param string $folder |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public function analyseFolder(string $appId, string $folder): array { |
|
| 83 | + $errors = []; |
|
| 84 | + |
|
| 85 | + $excludedDirectories = ['vendor', '3rdparty', '.git', 'l10n', 'tests', 'test', 'build']; |
|
| 86 | + if ($appId === 'password_policy') { |
|
| 87 | + $excludedDirectories[] = 'lists'; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $excludes = array_map(function($item) use ($folder) { |
|
| 91 | + return $folder . '/' . $item; |
|
| 92 | + }, $excludedDirectories); |
|
| 93 | + |
|
| 94 | + $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS); |
|
| 95 | + $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($excludes){ |
|
| 96 | + /** @var SplFileInfo $item */ |
|
| 97 | + foreach($excludes as $exclude) { |
|
| 98 | + if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + return true; |
|
| 103 | + }); |
|
| 104 | + $iterator = new RecursiveIteratorIterator($iterator); |
|
| 105 | + $iterator = new RegexIterator($iterator, '/^.+\.php$/i'); |
|
| 106 | + |
|
| 107 | + foreach ($iterator as $file) { |
|
| 108 | + /** @var SplFileInfo $file */ |
|
| 109 | + $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]); |
|
| 110 | + $fileErrors = $this->analyseFile($file->__toString()); |
|
| 111 | + $this->emit('CodeChecker', 'analyseFileFinished', [$file->getPathname(), $fileErrors]); |
|
| 112 | + $errors = array_merge($fileErrors, $errors); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $errors; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @param string $file |
|
| 121 | + * @return array |
|
| 122 | + */ |
|
| 123 | + public function analyseFile(string $file): array { |
|
| 124 | + $code = file_get_contents($file); |
|
| 125 | + $statements = $this->parser->parse($code); |
|
| 126 | + |
|
| 127 | + $visitor = new NodeVisitor($this->checkList); |
|
| 128 | + $migrationVisitor = new MigrationSchemaChecker(); |
|
| 129 | + $traverser = new NodeTraverser; |
|
| 130 | + $traverser->addVisitor($visitor); |
|
| 131 | + |
|
| 132 | + if ($this->checkMigrationSchema && preg_match('#^.+\\/Migration\\/Version[^\\/]{1,255}\\.php$#i', $file)) { |
|
| 133 | + $traverser->addVisitor($migrationVisitor); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $traverser->traverse($statements); |
|
| 137 | + |
|
| 138 | + return array_merge($visitor->errors, $migrationVisitor->errors); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -40,10 +40,10 @@ discard block |
||
| 40 | 40 | const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001; |
| 41 | 41 | const STATIC_CALL_NOT_ALLOWED = 1002; |
| 42 | 42 | const CLASS_CONST_FETCH_NOT_ALLOWED = 1003; |
| 43 | - const CLASS_NEW_NOT_ALLOWED = 1004; |
|
| 44 | - const OP_OPERATOR_USAGE_DISCOURAGED = 1005; |
|
| 45 | - const CLASS_USE_NOT_ALLOWED = 1006; |
|
| 46 | - const CLASS_METHOD_CALL_NOT_ALLOWED = 1007; |
|
| 43 | + const CLASS_NEW_NOT_ALLOWED = 1004; |
|
| 44 | + const OP_OPERATOR_USAGE_DISCOURAGED = 1005; |
|
| 45 | + const CLASS_USE_NOT_ALLOWED = 1006; |
|
| 46 | + const CLASS_METHOD_CALL_NOT_ALLOWED = 1007; |
|
| 47 | 47 | |
| 48 | 48 | /** @var Parser */ |
| 49 | 49 | private $parser; |
@@ -88,13 +88,13 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | $excludes = array_map(function($item) use ($folder) { |
| 91 | - return $folder . '/' . $item; |
|
| 91 | + return $folder.'/'.$item; |
|
| 92 | 92 | }, $excludedDirectories); |
| 93 | 93 | |
| 94 | 94 | $iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS); |
| 95 | 95 | $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($excludes){ |
| 96 | 96 | /** @var SplFileInfo $item */ |
| 97 | - foreach($excludes as $exclude) { |
|
| 97 | + foreach ($excludes as $exclude) { |
|
| 98 | 98 | if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) { |
| 99 | 99 | return false; |
| 100 | 100 | } |