@@ -28,116 +28,116 @@ |
||
| 28 | 28 | |
| 29 | 29 | class InfoChecker extends BasicEmitter { |
| 30 | 30 | |
| 31 | - /** @var InfoParser */ |
|
| 32 | - private $infoParser; |
|
| 33 | - |
|
| 34 | - private $mandatoryFields = [ |
|
| 35 | - 'author', |
|
| 36 | - 'description', |
|
| 37 | - 'dependencies', |
|
| 38 | - 'id', |
|
| 39 | - 'licence', |
|
| 40 | - 'name', |
|
| 41 | - 'version', |
|
| 42 | - ]; |
|
| 43 | - private $optionalFields = [ |
|
| 44 | - 'bugs', |
|
| 45 | - 'category', |
|
| 46 | - 'default_enable', |
|
| 47 | - 'documentation', |
|
| 48 | - 'namespace', |
|
| 49 | - 'ocsid', |
|
| 50 | - 'public', |
|
| 51 | - 'remote', |
|
| 52 | - 'repository', |
|
| 53 | - 'types', |
|
| 54 | - 'website', |
|
| 55 | - ]; |
|
| 56 | - private $deprecatedFields = [ |
|
| 57 | - 'info', |
|
| 58 | - 'require', |
|
| 59 | - 'requiremax', |
|
| 60 | - 'requiremin', |
|
| 61 | - 'shipped', |
|
| 62 | - 'standalone', |
|
| 63 | - ]; |
|
| 64 | - |
|
| 65 | - public function __construct(InfoParser $infoParser) { |
|
| 66 | - $this->infoParser = $infoParser; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param string $appId |
|
| 71 | - * @return array |
|
| 72 | - */ |
|
| 73 | - public function analyse($appId) { |
|
| 74 | - $appPath = \OC_App::getAppPath($appId); |
|
| 75 | - if ($appPath === false) { |
|
| 76 | - throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $errors = []; |
|
| 80 | - |
|
| 81 | - $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); |
|
| 82 | - |
|
| 83 | - if (!isset($info['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 84 | - $errors[] = [ |
|
| 85 | - 'type' => 'missingRequirement', |
|
| 86 | - 'field' => 'min', |
|
| 87 | - ]; |
|
| 88 | - $this->emit('InfoChecker', 'missingRequirement', ['min']); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if (!isset($info['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 92 | - $errors[] = [ |
|
| 93 | - 'type' => 'missingRequirement', |
|
| 94 | - 'field' => 'max', |
|
| 95 | - ]; |
|
| 96 | - $this->emit('InfoChecker', 'missingRequirement', ['max']); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - foreach ($info as $key => $value) { |
|
| 100 | - if(is_array($value)) { |
|
| 101 | - $value = json_encode($value); |
|
| 102 | - } |
|
| 103 | - if (in_array($key, $this->mandatoryFields)) { |
|
| 104 | - $this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]); |
|
| 105 | - continue; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if (in_array($key, $this->optionalFields)) { |
|
| 109 | - $this->emit('InfoChecker', 'optionalFieldFound', [$key, $value]); |
|
| 110 | - continue; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if (in_array($key, $this->deprecatedFields)) { |
|
| 114 | - // skip empty arrays - empty arrays for remote and public are always added |
|
| 115 | - if($value === '[]' && in_array($key, ['public', 'remote', 'info'])) { |
|
| 116 | - continue; |
|
| 117 | - } |
|
| 118 | - $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); |
|
| 119 | - continue; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $this->emit('InfoChecker', 'unusedFieldFound', [$key, $value]); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - foreach ($this->mandatoryFields as $key) { |
|
| 126 | - if(!isset($info[$key])) { |
|
| 127 | - $this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]); |
|
| 128 | - $errors[] = [ |
|
| 129 | - 'type' => 'mandatoryFieldMissing', |
|
| 130 | - 'field' => $key, |
|
| 131 | - ]; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $versionFile = $appPath . '/appinfo/version'; |
|
| 136 | - if (is_file($versionFile)) { |
|
| 137 | - $version = trim(file_get_contents($versionFile)); |
|
| 138 | - $this->emit('InfoChecker', 'migrateVersion', [$version]); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return $errors; |
|
| 142 | - } |
|
| 31 | + /** @var InfoParser */ |
|
| 32 | + private $infoParser; |
|
| 33 | + |
|
| 34 | + private $mandatoryFields = [ |
|
| 35 | + 'author', |
|
| 36 | + 'description', |
|
| 37 | + 'dependencies', |
|
| 38 | + 'id', |
|
| 39 | + 'licence', |
|
| 40 | + 'name', |
|
| 41 | + 'version', |
|
| 42 | + ]; |
|
| 43 | + private $optionalFields = [ |
|
| 44 | + 'bugs', |
|
| 45 | + 'category', |
|
| 46 | + 'default_enable', |
|
| 47 | + 'documentation', |
|
| 48 | + 'namespace', |
|
| 49 | + 'ocsid', |
|
| 50 | + 'public', |
|
| 51 | + 'remote', |
|
| 52 | + 'repository', |
|
| 53 | + 'types', |
|
| 54 | + 'website', |
|
| 55 | + ]; |
|
| 56 | + private $deprecatedFields = [ |
|
| 57 | + 'info', |
|
| 58 | + 'require', |
|
| 59 | + 'requiremax', |
|
| 60 | + 'requiremin', |
|
| 61 | + 'shipped', |
|
| 62 | + 'standalone', |
|
| 63 | + ]; |
|
| 64 | + |
|
| 65 | + public function __construct(InfoParser $infoParser) { |
|
| 66 | + $this->infoParser = $infoParser; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param string $appId |
|
| 71 | + * @return array |
|
| 72 | + */ |
|
| 73 | + public function analyse($appId) { |
|
| 74 | + $appPath = \OC_App::getAppPath($appId); |
|
| 75 | + if ($appPath === false) { |
|
| 76 | + throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $errors = []; |
|
| 80 | + |
|
| 81 | + $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); |
|
| 82 | + |
|
| 83 | + if (!isset($info['dependencies']['nextcloud']['@attributes']['min-version'])) { |
|
| 84 | + $errors[] = [ |
|
| 85 | + 'type' => 'missingRequirement', |
|
| 86 | + 'field' => 'min', |
|
| 87 | + ]; |
|
| 88 | + $this->emit('InfoChecker', 'missingRequirement', ['min']); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if (!isset($info['dependencies']['nextcloud']['@attributes']['max-version'])) { |
|
| 92 | + $errors[] = [ |
|
| 93 | + 'type' => 'missingRequirement', |
|
| 94 | + 'field' => 'max', |
|
| 95 | + ]; |
|
| 96 | + $this->emit('InfoChecker', 'missingRequirement', ['max']); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + foreach ($info as $key => $value) { |
|
| 100 | + if(is_array($value)) { |
|
| 101 | + $value = json_encode($value); |
|
| 102 | + } |
|
| 103 | + if (in_array($key, $this->mandatoryFields)) { |
|
| 104 | + $this->emit('InfoChecker', 'mandatoryFieldFound', [$key, $value]); |
|
| 105 | + continue; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if (in_array($key, $this->optionalFields)) { |
|
| 109 | + $this->emit('InfoChecker', 'optionalFieldFound', [$key, $value]); |
|
| 110 | + continue; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if (in_array($key, $this->deprecatedFields)) { |
|
| 114 | + // skip empty arrays - empty arrays for remote and public are always added |
|
| 115 | + if($value === '[]' && in_array($key, ['public', 'remote', 'info'])) { |
|
| 116 | + continue; |
|
| 117 | + } |
|
| 118 | + $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); |
|
| 119 | + continue; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $this->emit('InfoChecker', 'unusedFieldFound', [$key, $value]); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + foreach ($this->mandatoryFields as $key) { |
|
| 126 | + if(!isset($info[$key])) { |
|
| 127 | + $this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]); |
|
| 128 | + $errors[] = [ |
|
| 129 | + 'type' => 'mandatoryFieldMissing', |
|
| 130 | + 'field' => $key, |
|
| 131 | + ]; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $versionFile = $appPath . '/appinfo/version'; |
|
| 136 | + if (is_file($versionFile)) { |
|
| 137 | + $version = trim(file_get_contents($versionFile)); |
|
| 138 | + $this->emit('InfoChecker', 'migrateVersion', [$version]); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return $errors; |
|
| 142 | + } |
|
| 143 | 143 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | $errors = []; |
| 80 | 80 | |
| 81 | - $info = $this->infoParser->parse($appPath . '/appinfo/info.xml'); |
|
| 81 | + $info = $this->infoParser->parse($appPath.'/appinfo/info.xml'); |
|
| 82 | 82 | |
| 83 | 83 | if (!isset($info['dependencies']['nextcloud']['@attributes']['min-version'])) { |
| 84 | 84 | $errors[] = [ |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | foreach ($info as $key => $value) { |
| 100 | - if(is_array($value)) { |
|
| 100 | + if (is_array($value)) { |
|
| 101 | 101 | $value = json_encode($value); |
| 102 | 102 | } |
| 103 | 103 | if (in_array($key, $this->mandatoryFields)) { |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | if (in_array($key, $this->deprecatedFields)) { |
| 114 | 114 | // skip empty arrays - empty arrays for remote and public are always added |
| 115 | - if($value === '[]' && in_array($key, ['public', 'remote', 'info'])) { |
|
| 115 | + if ($value === '[]' && in_array($key, ['public', 'remote', 'info'])) { |
|
| 116 | 116 | continue; |
| 117 | 117 | } |
| 118 | 118 | $this->emit('InfoChecker', 'deprecatedFieldFound', [$key, $value]); |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | foreach ($this->mandatoryFields as $key) { |
| 126 | - if(!isset($info[$key])) { |
|
| 126 | + if (!isset($info[$key])) { |
|
| 127 | 127 | $this->emit('InfoChecker', 'mandatoryFieldMissing', [$key]); |
| 128 | 128 | $errors[] = [ |
| 129 | 129 | 'type' => 'mandatoryFieldMissing', |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - $versionFile = $appPath . '/appinfo/version'; |
|
| 135 | + $versionFile = $appPath.'/appinfo/version'; |
|
| 136 | 136 | if (is_file($versionFile)) { |
| 137 | 137 | $version = trim(file_get_contents($versionFile)); |
| 138 | 138 | $this->emit('InfoChecker', 'migrateVersion', [$version]); |
@@ -41,214 +41,214 @@ |
||
| 41 | 41 | |
| 42 | 42 | class CheckCode extends Command implements CompletionAwareInterface { |
| 43 | 43 | |
| 44 | - /** @var InfoParser */ |
|
| 45 | - private $infoParser; |
|
| 46 | - |
|
| 47 | - protected $checkers = [ |
|
| 48 | - 'private' => '\OC\App\CodeChecker\PrivateCheck', |
|
| 49 | - 'deprecation' => '\OC\App\CodeChecker\DeprecationCheck', |
|
| 50 | - 'strong-comparison' => '\OC\App\CodeChecker\StrongComparisonCheck', |
|
| 51 | - ]; |
|
| 52 | - |
|
| 53 | - public function __construct(InfoParser $infoParser) { |
|
| 54 | - parent::__construct(); |
|
| 55 | - $this->infoParser = $infoParser; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - protected function configure() { |
|
| 59 | - $this |
|
| 60 | - ->setName('app:check-code') |
|
| 61 | - ->setDescription('check code to be compliant') |
|
| 62 | - ->addArgument( |
|
| 63 | - 'app-id', |
|
| 64 | - InputArgument::REQUIRED, |
|
| 65 | - 'check the specified app' |
|
| 66 | - ) |
|
| 67 | - ->addOption( |
|
| 68 | - 'checker', |
|
| 69 | - 'c', |
|
| 70 | - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 71 | - 'enable the specified checker(s)', |
|
| 72 | - [ 'private', 'deprecation', 'strong-comparison' ] |
|
| 73 | - ) |
|
| 74 | - ->addOption( |
|
| 75 | - '--skip-checkers', |
|
| 76 | - null, |
|
| 77 | - InputOption::VALUE_NONE, |
|
| 78 | - 'skips the the code checkers to only check info.xml, language and database schema' |
|
| 79 | - ) |
|
| 80 | - ->addOption( |
|
| 81 | - '--skip-validate-info', |
|
| 82 | - null, |
|
| 83 | - InputOption::VALUE_NONE, |
|
| 84 | - 'skips the info.xml/version check' |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 89 | - $appId = $input->getArgument('app-id'); |
|
| 90 | - |
|
| 91 | - $checkList = new EmptyCheck(); |
|
| 92 | - foreach ($input->getOption('checker') as $checker) { |
|
| 93 | - if (!isset($this->checkers[$checker])) { |
|
| 94 | - throw new \InvalidArgumentException('Invalid checker: '.$checker); |
|
| 95 | - } |
|
| 96 | - $checkerClass = $this->checkers[$checker]; |
|
| 97 | - $checkList = new $checkerClass($checkList); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $codeChecker = new CodeChecker($checkList); |
|
| 101 | - |
|
| 102 | - $codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) { |
|
| 103 | - if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 104 | - $output->writeln("<info>Analysing {$params}</info>"); |
|
| 105 | - } |
|
| 106 | - }); |
|
| 107 | - $codeChecker->listen('CodeChecker', 'analyseFileFinished', function($filename, $errors) use ($output) { |
|
| 108 | - $count = count($errors); |
|
| 109 | - |
|
| 110 | - // show filename if the verbosity is low, but there are errors in a file |
|
| 111 | - if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) { |
|
| 112 | - $output->writeln("<info>Analysing {$filename}</info>"); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // show error count if there are errors present or the verbosity is high |
|
| 116 | - if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 117 | - $output->writeln(" {$count} errors"); |
|
| 118 | - } |
|
| 119 | - usort($errors, function($a, $b) { |
|
| 120 | - return $a['line'] >$b['line']; |
|
| 121 | - }); |
|
| 122 | - |
|
| 123 | - foreach($errors as $p) { |
|
| 124 | - $line = sprintf("%' 4d", $p['line']); |
|
| 125 | - $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>"); |
|
| 126 | - } |
|
| 127 | - }); |
|
| 128 | - $errors = []; |
|
| 129 | - if(!$input->getOption('skip-checkers')) { |
|
| 130 | - $errors = $codeChecker->analyse($appId); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if(!$input->getOption('skip-validate-info')) { |
|
| 134 | - $infoChecker = new InfoChecker($this->infoParser); |
|
| 135 | - |
|
| 136 | - $infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) { |
|
| 137 | - $output->writeln("<error>Mandatory field missing: $key</error>"); |
|
| 138 | - }); |
|
| 139 | - |
|
| 140 | - $infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) { |
|
| 141 | - if($value === [] || is_null($value) || $value === '') { |
|
| 142 | - $output->writeln("<info>Deprecated field available: $key</info>"); |
|
| 143 | - } else { |
|
| 144 | - $output->writeln("<info>Deprecated field available: $key => $value</info>"); |
|
| 145 | - } |
|
| 146 | - }); |
|
| 147 | - |
|
| 148 | - $infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) { |
|
| 149 | - $output->writeln("<error>Nextcloud $minMax version requirement missing</error>"); |
|
| 150 | - }); |
|
| 151 | - |
|
| 152 | - $infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) { |
|
| 153 | - $output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>"); |
|
| 154 | - }); |
|
| 155 | - |
|
| 156 | - $infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) { |
|
| 157 | - $output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>"); |
|
| 158 | - }); |
|
| 159 | - |
|
| 160 | - $infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) { |
|
| 161 | - $output->writeln("<error>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</error>"); |
|
| 162 | - }); |
|
| 163 | - |
|
| 164 | - if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 165 | - $infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) { |
|
| 166 | - $output->writeln("<info>Mandatory field available: $key => $value</info>"); |
|
| 167 | - }); |
|
| 168 | - |
|
| 169 | - $infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) { |
|
| 170 | - $output->writeln("<info>Optional field available: $key => $value</info>"); |
|
| 171 | - }); |
|
| 172 | - |
|
| 173 | - $infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) { |
|
| 174 | - $output->writeln("<info>Unused field available: $key => $value</info>"); |
|
| 175 | - }); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - $infoErrors = $infoChecker->analyse($appId); |
|
| 179 | - |
|
| 180 | - $errors = array_merge($errors, $infoErrors); |
|
| 181 | - |
|
| 182 | - $languageParser = new LanguageParseChecker(); |
|
| 183 | - $languageErrors = $languageParser->analyse($appId); |
|
| 184 | - |
|
| 185 | - foreach ($languageErrors as $languageError) { |
|
| 186 | - $output->writeln("<error>$languageError</error>"); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $errors = array_merge($errors, $languageErrors); |
|
| 190 | - |
|
| 191 | - $databaseSchema = new DatabaseSchemaChecker(); |
|
| 192 | - $schemaErrors = $databaseSchema->analyse($appId); |
|
| 193 | - |
|
| 194 | - foreach ($schemaErrors['errors'] as $schemaError) { |
|
| 195 | - $output->writeln("<error>$schemaError</error>"); |
|
| 196 | - } |
|
| 197 | - foreach ($schemaErrors['warnings'] as $schemaWarning) { |
|
| 198 | - $output->writeln("<comment>$schemaWarning</comment>"); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $errors = array_merge($errors, $schemaErrors['errors']); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $this->analyseUpdateFile($appId, $output); |
|
| 205 | - |
|
| 206 | - if (empty($errors)) { |
|
| 207 | - $output->writeln('<info>App is compliant - awesome job!</info>'); |
|
| 208 | - return 0; |
|
| 209 | - } else { |
|
| 210 | - $output->writeln('<error>App is not compliant</error>'); |
|
| 211 | - return 101; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @param string $appId |
|
| 217 | - * @param $output |
|
| 218 | - */ |
|
| 219 | - private function analyseUpdateFile($appId, OutputInterface $output) { |
|
| 220 | - $appPath = \OC_App::getAppPath($appId); |
|
| 221 | - if ($appPath === false) { |
|
| 222 | - throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $updatePhp = $appPath . '/appinfo/update.php'; |
|
| 226 | - if (file_exists($updatePhp)) { |
|
| 227 | - $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>"); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @param string $optionName |
|
| 233 | - * @param CompletionContext $context |
|
| 234 | - * @return string[] |
|
| 235 | - */ |
|
| 236 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 237 | - if ($optionName === 'checker') { |
|
| 238 | - return ['private', 'deprecation', 'strong-comparison']; |
|
| 239 | - } |
|
| 240 | - return []; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @param string $argumentName |
|
| 245 | - * @param CompletionContext $context |
|
| 246 | - * @return string[] |
|
| 247 | - */ |
|
| 248 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 249 | - if ($argumentName === 'app-id') { |
|
| 250 | - return \OC_App::getAllApps(); |
|
| 251 | - } |
|
| 252 | - return []; |
|
| 253 | - } |
|
| 44 | + /** @var InfoParser */ |
|
| 45 | + private $infoParser; |
|
| 46 | + |
|
| 47 | + protected $checkers = [ |
|
| 48 | + 'private' => '\OC\App\CodeChecker\PrivateCheck', |
|
| 49 | + 'deprecation' => '\OC\App\CodeChecker\DeprecationCheck', |
|
| 50 | + 'strong-comparison' => '\OC\App\CodeChecker\StrongComparisonCheck', |
|
| 51 | + ]; |
|
| 52 | + |
|
| 53 | + public function __construct(InfoParser $infoParser) { |
|
| 54 | + parent::__construct(); |
|
| 55 | + $this->infoParser = $infoParser; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + protected function configure() { |
|
| 59 | + $this |
|
| 60 | + ->setName('app:check-code') |
|
| 61 | + ->setDescription('check code to be compliant') |
|
| 62 | + ->addArgument( |
|
| 63 | + 'app-id', |
|
| 64 | + InputArgument::REQUIRED, |
|
| 65 | + 'check the specified app' |
|
| 66 | + ) |
|
| 67 | + ->addOption( |
|
| 68 | + 'checker', |
|
| 69 | + 'c', |
|
| 70 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 71 | + 'enable the specified checker(s)', |
|
| 72 | + [ 'private', 'deprecation', 'strong-comparison' ] |
|
| 73 | + ) |
|
| 74 | + ->addOption( |
|
| 75 | + '--skip-checkers', |
|
| 76 | + null, |
|
| 77 | + InputOption::VALUE_NONE, |
|
| 78 | + 'skips the the code checkers to only check info.xml, language and database schema' |
|
| 79 | + ) |
|
| 80 | + ->addOption( |
|
| 81 | + '--skip-validate-info', |
|
| 82 | + null, |
|
| 83 | + InputOption::VALUE_NONE, |
|
| 84 | + 'skips the info.xml/version check' |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 89 | + $appId = $input->getArgument('app-id'); |
|
| 90 | + |
|
| 91 | + $checkList = new EmptyCheck(); |
|
| 92 | + foreach ($input->getOption('checker') as $checker) { |
|
| 93 | + if (!isset($this->checkers[$checker])) { |
|
| 94 | + throw new \InvalidArgumentException('Invalid checker: '.$checker); |
|
| 95 | + } |
|
| 96 | + $checkerClass = $this->checkers[$checker]; |
|
| 97 | + $checkList = new $checkerClass($checkList); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $codeChecker = new CodeChecker($checkList); |
|
| 101 | + |
|
| 102 | + $codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) { |
|
| 103 | + if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 104 | + $output->writeln("<info>Analysing {$params}</info>"); |
|
| 105 | + } |
|
| 106 | + }); |
|
| 107 | + $codeChecker->listen('CodeChecker', 'analyseFileFinished', function($filename, $errors) use ($output) { |
|
| 108 | + $count = count($errors); |
|
| 109 | + |
|
| 110 | + // show filename if the verbosity is low, but there are errors in a file |
|
| 111 | + if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) { |
|
| 112 | + $output->writeln("<info>Analysing {$filename}</info>"); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // show error count if there are errors present or the verbosity is high |
|
| 116 | + if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 117 | + $output->writeln(" {$count} errors"); |
|
| 118 | + } |
|
| 119 | + usort($errors, function($a, $b) { |
|
| 120 | + return $a['line'] >$b['line']; |
|
| 121 | + }); |
|
| 122 | + |
|
| 123 | + foreach($errors as $p) { |
|
| 124 | + $line = sprintf("%' 4d", $p['line']); |
|
| 125 | + $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>"); |
|
| 126 | + } |
|
| 127 | + }); |
|
| 128 | + $errors = []; |
|
| 129 | + if(!$input->getOption('skip-checkers')) { |
|
| 130 | + $errors = $codeChecker->analyse($appId); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if(!$input->getOption('skip-validate-info')) { |
|
| 134 | + $infoChecker = new InfoChecker($this->infoParser); |
|
| 135 | + |
|
| 136 | + $infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) { |
|
| 137 | + $output->writeln("<error>Mandatory field missing: $key</error>"); |
|
| 138 | + }); |
|
| 139 | + |
|
| 140 | + $infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) { |
|
| 141 | + if($value === [] || is_null($value) || $value === '') { |
|
| 142 | + $output->writeln("<info>Deprecated field available: $key</info>"); |
|
| 143 | + } else { |
|
| 144 | + $output->writeln("<info>Deprecated field available: $key => $value</info>"); |
|
| 145 | + } |
|
| 146 | + }); |
|
| 147 | + |
|
| 148 | + $infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) { |
|
| 149 | + $output->writeln("<error>Nextcloud $minMax version requirement missing</error>"); |
|
| 150 | + }); |
|
| 151 | + |
|
| 152 | + $infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) { |
|
| 153 | + $output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>"); |
|
| 154 | + }); |
|
| 155 | + |
|
| 156 | + $infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) { |
|
| 157 | + $output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>"); |
|
| 158 | + }); |
|
| 159 | + |
|
| 160 | + $infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) { |
|
| 161 | + $output->writeln("<error>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</error>"); |
|
| 162 | + }); |
|
| 163 | + |
|
| 164 | + if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 165 | + $infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) { |
|
| 166 | + $output->writeln("<info>Mandatory field available: $key => $value</info>"); |
|
| 167 | + }); |
|
| 168 | + |
|
| 169 | + $infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) { |
|
| 170 | + $output->writeln("<info>Optional field available: $key => $value</info>"); |
|
| 171 | + }); |
|
| 172 | + |
|
| 173 | + $infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) { |
|
| 174 | + $output->writeln("<info>Unused field available: $key => $value</info>"); |
|
| 175 | + }); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + $infoErrors = $infoChecker->analyse($appId); |
|
| 179 | + |
|
| 180 | + $errors = array_merge($errors, $infoErrors); |
|
| 181 | + |
|
| 182 | + $languageParser = new LanguageParseChecker(); |
|
| 183 | + $languageErrors = $languageParser->analyse($appId); |
|
| 184 | + |
|
| 185 | + foreach ($languageErrors as $languageError) { |
|
| 186 | + $output->writeln("<error>$languageError</error>"); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $errors = array_merge($errors, $languageErrors); |
|
| 190 | + |
|
| 191 | + $databaseSchema = new DatabaseSchemaChecker(); |
|
| 192 | + $schemaErrors = $databaseSchema->analyse($appId); |
|
| 193 | + |
|
| 194 | + foreach ($schemaErrors['errors'] as $schemaError) { |
|
| 195 | + $output->writeln("<error>$schemaError</error>"); |
|
| 196 | + } |
|
| 197 | + foreach ($schemaErrors['warnings'] as $schemaWarning) { |
|
| 198 | + $output->writeln("<comment>$schemaWarning</comment>"); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $errors = array_merge($errors, $schemaErrors['errors']); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $this->analyseUpdateFile($appId, $output); |
|
| 205 | + |
|
| 206 | + if (empty($errors)) { |
|
| 207 | + $output->writeln('<info>App is compliant - awesome job!</info>'); |
|
| 208 | + return 0; |
|
| 209 | + } else { |
|
| 210 | + $output->writeln('<error>App is not compliant</error>'); |
|
| 211 | + return 101; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @param string $appId |
|
| 217 | + * @param $output |
|
| 218 | + */ |
|
| 219 | + private function analyseUpdateFile($appId, OutputInterface $output) { |
|
| 220 | + $appPath = \OC_App::getAppPath($appId); |
|
| 221 | + if ($appPath === false) { |
|
| 222 | + throw new \RuntimeException("No app with given id <$appId> known."); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $updatePhp = $appPath . '/appinfo/update.php'; |
|
| 226 | + if (file_exists($updatePhp)) { |
|
| 227 | + $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>"); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @param string $optionName |
|
| 233 | + * @param CompletionContext $context |
|
| 234 | + * @return string[] |
|
| 235 | + */ |
|
| 236 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 237 | + if ($optionName === 'checker') { |
|
| 238 | + return ['private', 'deprecation', 'strong-comparison']; |
|
| 239 | + } |
|
| 240 | + return []; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @param string $argumentName |
|
| 245 | + * @param CompletionContext $context |
|
| 246 | + * @return string[] |
|
| 247 | + */ |
|
| 248 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 249 | + if ($argumentName === 'app-id') { |
|
| 250 | + return \OC_App::getAllApps(); |
|
| 251 | + } |
|
| 252 | + return []; |
|
| 253 | + } |
|
| 254 | 254 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | use Symfony\Component\Console\Input\InputOption; |
| 40 | 40 | use Symfony\Component\Console\Output\OutputInterface; |
| 41 | 41 | |
| 42 | -class CheckCode extends Command implements CompletionAwareInterface { |
|
| 42 | +class CheckCode extends Command implements CompletionAwareInterface { |
|
| 43 | 43 | |
| 44 | 44 | /** @var InfoParser */ |
| 45 | 45 | private $infoParser; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | 'c', |
| 70 | 70 | InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 71 | 71 | 'enable the specified checker(s)', |
| 72 | - [ 'private', 'deprecation', 'strong-comparison' ] |
|
| 72 | + ['private', 'deprecation', 'strong-comparison'] |
|
| 73 | 73 | ) |
| 74 | 74 | ->addOption( |
| 75 | 75 | '--skip-checkers', |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $codeChecker = new CodeChecker($checkList); |
| 101 | 101 | |
| 102 | 102 | $codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) { |
| 103 | - if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 103 | + if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 104 | 104 | $output->writeln("<info>Analysing {$params}</info>"); |
| 105 | 105 | } |
| 106 | 106 | }); |
@@ -108,29 +108,29 @@ discard block |
||
| 108 | 108 | $count = count($errors); |
| 109 | 109 | |
| 110 | 110 | // show filename if the verbosity is low, but there are errors in a file |
| 111 | - if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) { |
|
| 111 | + if ($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) { |
|
| 112 | 112 | $output->writeln("<info>Analysing {$filename}</info>"); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | // show error count if there are errors present or the verbosity is high |
| 116 | - if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 116 | + if ($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 117 | 117 | $output->writeln(" {$count} errors"); |
| 118 | 118 | } |
| 119 | 119 | usort($errors, function($a, $b) { |
| 120 | - return $a['line'] >$b['line']; |
|
| 120 | + return $a['line'] > $b['line']; |
|
| 121 | 121 | }); |
| 122 | 122 | |
| 123 | - foreach($errors as $p) { |
|
| 123 | + foreach ($errors as $p) { |
|
| 124 | 124 | $line = sprintf("%' 4d", $p['line']); |
| 125 | 125 | $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>"); |
| 126 | 126 | } |
| 127 | 127 | }); |
| 128 | 128 | $errors = []; |
| 129 | - if(!$input->getOption('skip-checkers')) { |
|
| 129 | + if (!$input->getOption('skip-checkers')) { |
|
| 130 | 130 | $errors = $codeChecker->analyse($appId); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - if(!$input->getOption('skip-validate-info')) { |
|
| 133 | + if (!$input->getOption('skip-validate-info')) { |
|
| 134 | 134 | $infoChecker = new InfoChecker($this->infoParser); |
| 135 | 135 | |
| 136 | 136 | $infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) { |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | }); |
| 139 | 139 | |
| 140 | 140 | $infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) { |
| 141 | - if($value === [] || is_null($value) || $value === '') { |
|
| 141 | + if ($value === [] || is_null($value) || $value === '') { |
|
| 142 | 142 | $output->writeln("<info>Deprecated field available: $key</info>"); |
| 143 | 143 | } else { |
| 144 | 144 | $output->writeln("<info>Deprecated field available: $key => $value</info>"); |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | $output->writeln("<error>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</error>"); |
| 162 | 162 | }); |
| 163 | 163 | |
| 164 | - if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 164 | + if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
|
| 165 | 165 | $infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) { |
| 166 | 166 | $output->writeln("<info>Mandatory field available: $key => $value</info>"); |
| 167 | 167 | }); |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | throw new \RuntimeException("No app with given id <$appId> known."); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | - $updatePhp = $appPath . '/appinfo/update.php'; |
|
| 225 | + $updatePhp = $appPath.'/appinfo/update.php'; |
|
| 226 | 226 | if (file_exists($updatePhp)) { |
| 227 | 227 | $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>"); |
| 228 | 228 | } |