CheckCode.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Robin McCorkell <robin@mccorkell.me.uk>
  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\Core\Command\App;
  26. use OC\App\CodeChecker\CodeChecker;
  27. use OC\App\CodeChecker\EmptyCheck;
  28. use OC\App\CodeChecker\InfoChecker;
  29. use OC\App\InfoParser;
  30. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  31. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class CheckCode extends Command implements CompletionAwareInterface {
  38. /** @var InfoParser */
  39. private $infoParser;
  40. protected $checkers = [
  41. 'private' => '\OC\App\CodeChecker\PrivateCheck',
  42. 'deprecation' => '\OC\App\CodeChecker\DeprecationCheck',
  43. 'strong-comparison' => '\OC\App\CodeChecker\StrongComparisonCheck',
  44. ];
  45. public function __construct(InfoParser $infoParser) {
  46. parent::__construct();
  47. $this->infoParser = $infoParser;
  48. }
  49. protected function configure() {
  50. $this
  51. ->setName('app:check-code')
  52. ->setDescription('check code to be compliant')
  53. ->addArgument(
  54. 'app-id',
  55. InputArgument::REQUIRED,
  56. 'check the specified app'
  57. )
  58. ->addOption(
  59. 'checker',
  60. 'c',
  61. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  62. 'enable the specified checker(s)',
  63. [ 'private', 'deprecation', 'strong-comparison' ]
  64. )
  65. ->addOption(
  66. '--skip-validate-info',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'skips the info.xml/version check'
  70. );
  71. }
  72. protected function execute(InputInterface $input, OutputInterface $output) {
  73. $appId = $input->getArgument('app-id');
  74. $checkList = new EmptyCheck();
  75. foreach ($input->getOption('checker') as $checker) {
  76. if (!isset($this->checkers[$checker])) {
  77. throw new \InvalidArgumentException('Invalid checker: '.$checker);
  78. }
  79. $checkerClass = $this->checkers[$checker];
  80. $checkList = new $checkerClass($checkList);
  81. }
  82. $codeChecker = new CodeChecker($checkList);
  83. $codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) {
  84. if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  85. $output->writeln("<info>Analysing {$params}</info>");
  86. }
  87. });
  88. $codeChecker->listen('CodeChecker', 'analyseFileFinished', function($filename, $errors) use ($output) {
  89. $count = count($errors);
  90. // show filename if the verbosity is low, but there are errors in a file
  91. if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
  92. $output->writeln("<info>Analysing {$filename}</info>");
  93. }
  94. // show error count if there are errors present or the verbosity is high
  95. if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  96. $output->writeln(" {$count} errors");
  97. }
  98. usort($errors, function($a, $b) {
  99. return $a['line'] >$b['line'];
  100. });
  101. foreach($errors as $p) {
  102. $line = sprintf("%' 4d", $p['line']);
  103. $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>");
  104. }
  105. });
  106. $errors = $codeChecker->analyse($appId);
  107. if(!$input->getOption('skip-validate-info')) {
  108. $infoChecker = new InfoChecker($this->infoParser);
  109. $infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
  110. $output->writeln("<error>Mandatory field missing: $key</error>");
  111. });
  112. $infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
  113. if($value === [] || is_null($value) || $value === '') {
  114. $output->writeln("<info>Deprecated field available: $key</info>");
  115. } else {
  116. $output->writeln("<info>Deprecated field available: $key => $value</info>");
  117. }
  118. });
  119. $infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) {
  120. $output->writeln("<comment>Nextcloud $minMax version requirement missing (will be an error in Nextcloud 12 and later)</comment>");
  121. });
  122. $infoChecker->listen('InfoChecker', 'duplicateRequirement', function($minMax) use ($output) {
  123. $output->writeln("<error>Duplicate $minMax ownCloud version requirement found</error>");
  124. });
  125. $infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) {
  126. $output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>");
  127. });
  128. $infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) {
  129. $output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>");
  130. });
  131. $infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) {
  132. $output->writeln("<info>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</info>");
  133. });
  134. if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  135. $infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
  136. $output->writeln("<info>Mandatory field available: $key => $value</info>");
  137. });
  138. $infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) {
  139. $output->writeln("<info>Optional field available: $key => $value</info>");
  140. });
  141. $infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) {
  142. $output->writeln("<info>Unused field available: $key => $value</info>");
  143. });
  144. }
  145. $infoErrors = $infoChecker->analyse($appId);
  146. $errors = array_merge($errors, $infoErrors);
  147. }
  148. $this->analyseUpdateFile($appId, $output);
  149. if (empty($errors)) {
  150. $output->writeln('<info>App is compliant - awesome job!</info>');
  151. return 0;
  152. } else {
  153. $output->writeln('<error>App is not compliant</error>');
  154. return 101;
  155. }
  156. }
  157. /**
  158. * @param string $appId
  159. * @param $output
  160. */
  161. private function analyseUpdateFile($appId, OutputInterface $output) {
  162. $appPath = \OC_App::getAppPath($appId);
  163. if ($appPath === false) {
  164. throw new \RuntimeException("No app with given id <$appId> known.");
  165. }
  166. $updatePhp = $appPath . '/appinfo/update.php';
  167. if (file_exists($updatePhp)) {
  168. $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
  169. }
  170. }
  171. /**
  172. * @param string $optionName
  173. * @param CompletionContext $context
  174. * @return string[]
  175. */
  176. public function completeOptionValues($optionName, CompletionContext $context) {
  177. if ($optionName === 'checker') {
  178. return ['private', 'deprecation', 'strong-comparison'];
  179. }
  180. return [];
  181. }
  182. /**
  183. * @param string $argumentName
  184. * @param CompletionContext $context
  185. * @return string[]
  186. */
  187. public function completeArgumentValues($argumentName, CompletionContext $context) {
  188. if ($argumentName === 'app-id') {
  189. return \OC_App::getAllApps();
  190. }
  191. return [];
  192. }
  193. }