CheckCode.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\App;
  28. use OC\App\CodeChecker\CodeChecker;
  29. use OC\App\CodeChecker\DatabaseSchemaChecker;
  30. use OC\App\CodeChecker\DeprecationCheck;
  31. use OC\App\CodeChecker\EmptyCheck;
  32. use OC\App\CodeChecker\InfoChecker;
  33. use OC\App\CodeChecker\LanguageParseChecker;
  34. use OC\App\CodeChecker\PrivateCheck;
  35. use OC\App\CodeChecker\StrongComparisonCheck;
  36. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  37. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  38. use Symfony\Component\Console\Command\Command;
  39. use Symfony\Component\Console\Input\InputArgument;
  40. use Symfony\Component\Console\Input\InputInterface;
  41. use Symfony\Component\Console\Input\InputOption;
  42. use Symfony\Component\Console\Output\OutputInterface;
  43. class CheckCode extends Command implements CompletionAwareInterface {
  44. protected $checkers = [
  45. 'private' => PrivateCheck::class,
  46. 'deprecation' => DeprecationCheck::class,
  47. 'strong-comparison' => StrongComparisonCheck::class,
  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-checkers',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'skips the the code checkers to only check info.xml, language and database schema'
  70. )
  71. ->addOption(
  72. '--skip-validate-info',
  73. null,
  74. InputOption::VALUE_NONE,
  75. 'skips the info.xml/version check'
  76. );
  77. }
  78. protected function execute(InputInterface $input, OutputInterface $output): int {
  79. $appId = $input->getArgument('app-id');
  80. $checkList = new EmptyCheck();
  81. foreach ($input->getOption('checker') as $checker) {
  82. if (!isset($this->checkers[$checker])) {
  83. throw new \InvalidArgumentException('Invalid checker: '.$checker);
  84. }
  85. $checkerClass = $this->checkers[$checker];
  86. $checkList = new $checkerClass($checkList);
  87. }
  88. $codeChecker = new CodeChecker($checkList, !$input->getOption('skip-validate-info'));
  89. $codeChecker->listen('CodeChecker', 'analyseFileBegin', function ($params) use ($output) {
  90. if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  91. $output->writeln("<info>Analysing {$params}</info>");
  92. }
  93. });
  94. $codeChecker->listen('CodeChecker', 'analyseFileFinished', function ($filename, $errors) use ($output) {
  95. $count = count($errors);
  96. // show filename if the verbosity is low, but there are errors in a file
  97. if ($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
  98. $output->writeln("<info>Analysing {$filename}</info>");
  99. }
  100. // show error count if there are errors present or the verbosity is high
  101. if ($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  102. $output->writeln(" {$count} errors");
  103. }
  104. usort($errors, function ($a, $b) {
  105. return $a['line'] > $b['line'];
  106. });
  107. foreach ($errors as $p) {
  108. $line = sprintf("%' 4d", $p['line']);
  109. $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>");
  110. }
  111. });
  112. $errors = [];
  113. if (!$input->getOption('skip-checkers')) {
  114. $errors = $codeChecker->analyse($appId);
  115. }
  116. if (!$input->getOption('skip-validate-info')) {
  117. $infoChecker = new InfoChecker();
  118. $infoChecker->listen('InfoChecker', 'parseError', function ($error) use ($output) {
  119. $output->writeln("<error>Invalid appinfo.xml file found: $error</error>");
  120. });
  121. $infoErrors = $infoChecker->analyse($appId);
  122. $errors = array_merge($errors, $infoErrors);
  123. $languageParser = new LanguageParseChecker();
  124. $languageErrors = $languageParser->analyse($appId);
  125. foreach ($languageErrors as $languageError) {
  126. $output->writeln("<error>$languageError</error>");
  127. }
  128. $errors = array_merge($errors, $languageErrors);
  129. $databaseSchema = new DatabaseSchemaChecker();
  130. $schemaErrors = $databaseSchema->analyse($appId);
  131. foreach ($schemaErrors['errors'] as $schemaError) {
  132. $output->writeln("<error>$schemaError</error>");
  133. }
  134. foreach ($schemaErrors['warnings'] as $schemaWarning) {
  135. $output->writeln("<comment>$schemaWarning</comment>");
  136. }
  137. $errors = array_merge($errors, $schemaErrors['errors']);
  138. }
  139. if (empty($errors)) {
  140. $output->writeln('<info>App is compliant - awesome job!</info>');
  141. return 0;
  142. } else {
  143. $output->writeln('<error>App is not compliant</error>');
  144. return 101;
  145. }
  146. }
  147. /**
  148. * @param string $optionName
  149. * @param CompletionContext $context
  150. * @return string[]
  151. */
  152. public function completeOptionValues($optionName, CompletionContext $context) {
  153. if ($optionName === 'checker') {
  154. return ['private', 'deprecation', 'strong-comparison'];
  155. }
  156. return [];
  157. }
  158. /**
  159. * @param string $argumentName
  160. * @param CompletionContext $context
  161. * @return string[]
  162. */
  163. public function completeArgumentValues($argumentName, CompletionContext $context) {
  164. if ($argumentName === 'app-id') {
  165. return \OC_App::getAllApps();
  166. }
  167. return [];
  168. }
  169. }