CheckCode.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\DatabaseSchemaChecker;
  28. use OC\App\CodeChecker\EmptyCheck;
  29. use OC\App\CodeChecker\InfoChecker;
  30. use OC\App\CodeChecker\LanguageParseChecker;
  31. use OC\App\InfoParser;
  32. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  33. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  34. use Symfony\Component\Console\Command\Command;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Input\InputOption;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. use OC\App\CodeChecker\StrongComparisonCheck;
  40. use OC\App\CodeChecker\DeprecationCheck;
  41. use OC\App\CodeChecker\PrivateCheck;
  42. class CheckCode extends Command implements CompletionAwareInterface {
  43. protected $checkers = [
  44. 'private' => PrivateCheck::class,
  45. 'deprecation' => DeprecationCheck::class,
  46. 'strong-comparison' => StrongComparisonCheck::class,
  47. ];
  48. protected function configure() {
  49. $this
  50. ->setName('app:check-code')
  51. ->setDescription('check code to be compliant')
  52. ->addArgument(
  53. 'app-id',
  54. InputArgument::REQUIRED,
  55. 'check the specified app'
  56. )
  57. ->addOption(
  58. 'checker',
  59. 'c',
  60. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  61. 'enable the specified checker(s)',
  62. [ 'private', 'deprecation', 'strong-comparison' ]
  63. )
  64. ->addOption(
  65. '--skip-checkers',
  66. null,
  67. InputOption::VALUE_NONE,
  68. 'skips the the code checkers to only check info.xml, language and database schema'
  69. )
  70. ->addOption(
  71. '--skip-validate-info',
  72. null,
  73. InputOption::VALUE_NONE,
  74. 'skips the info.xml/version check'
  75. );
  76. }
  77. protected function execute(InputInterface $input, OutputInterface $output) {
  78. $appId = $input->getArgument('app-id');
  79. $checkList = new EmptyCheck();
  80. foreach ($input->getOption('checker') as $checker) {
  81. if (!isset($this->checkers[$checker])) {
  82. throw new \InvalidArgumentException('Invalid checker: '.$checker);
  83. }
  84. $checkerClass = $this->checkers[$checker];
  85. $checkList = new $checkerClass($checkList);
  86. }
  87. $codeChecker = new CodeChecker($checkList, !$input->getOption('skip-validate-info'));
  88. $codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) {
  89. if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  90. $output->writeln("<info>Analysing {$params}</info>");
  91. }
  92. });
  93. $codeChecker->listen('CodeChecker', 'analyseFileFinished', function($filename, $errors) use ($output) {
  94. $count = count($errors);
  95. // show filename if the verbosity is low, but there are errors in a file
  96. if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
  97. $output->writeln("<info>Analysing {$filename}</info>");
  98. }
  99. // show error count if there are errors present or the verbosity is high
  100. if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  101. $output->writeln(" {$count} errors");
  102. }
  103. usort($errors, function($a, $b) {
  104. return $a['line'] >$b['line'];
  105. });
  106. foreach($errors as $p) {
  107. $line = sprintf("%' 4d", $p['line']);
  108. $output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>");
  109. }
  110. });
  111. $errors = [];
  112. if(!$input->getOption('skip-checkers')) {
  113. $errors = $codeChecker->analyse($appId);
  114. }
  115. if(!$input->getOption('skip-validate-info')) {
  116. $infoChecker = new InfoChecker();
  117. $infoChecker->listen('InfoChecker', 'parseError', function($error) use ($output) {
  118. $output->writeln("<error>Invalid appinfo.xml file found: $error</error>");
  119. });
  120. $infoErrors = $infoChecker->analyse($appId);
  121. $errors = array_merge($errors, $infoErrors);
  122. $languageParser = new LanguageParseChecker();
  123. $languageErrors = $languageParser->analyse($appId);
  124. foreach ($languageErrors as $languageError) {
  125. $output->writeln("<error>$languageError</error>");
  126. }
  127. $errors = array_merge($errors, $languageErrors);
  128. $databaseSchema = new DatabaseSchemaChecker();
  129. $schemaErrors = $databaseSchema->analyse($appId);
  130. foreach ($schemaErrors['errors'] as $schemaError) {
  131. $output->writeln("<error>$schemaError</error>");
  132. }
  133. foreach ($schemaErrors['warnings'] as $schemaWarning) {
  134. $output->writeln("<comment>$schemaWarning</comment>");
  135. }
  136. $errors = array_merge($errors, $schemaErrors['errors']);
  137. }
  138. $this->analyseUpdateFile($appId, $output);
  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 $appId
  149. * @param $output
  150. */
  151. private function analyseUpdateFile($appId, OutputInterface $output) {
  152. $appPath = \OC_App::getAppPath($appId);
  153. if ($appPath === false) {
  154. throw new \RuntimeException("No app with given id <$appId> known.");
  155. }
  156. $updatePhp = $appPath . '/appinfo/update.php';
  157. if (file_exists($updatePhp)) {
  158. $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
  159. }
  160. }
  161. /**
  162. * @param string $optionName
  163. * @param CompletionContext $context
  164. * @return string[]
  165. */
  166. public function completeOptionValues($optionName, CompletionContext $context) {
  167. if ($optionName === 'checker') {
  168. return ['private', 'deprecation', 'strong-comparison'];
  169. }
  170. return [];
  171. }
  172. /**
  173. * @param string $argumentName
  174. * @param CompletionContext $context
  175. * @return string[]
  176. */
  177. public function completeArgumentValues($argumentName, CompletionContext $context) {
  178. if ($argumentName === 'app-id') {
  179. return \OC_App::getAllApps();
  180. }
  181. return [];
  182. }
  183. }