CheckCode.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\App;
  27. use OC\App\CodeChecker\CodeChecker;
  28. use OC\App\CodeChecker\DatabaseSchemaChecker;
  29. use OC\App\CodeChecker\DeprecationCheck;
  30. use OC\App\CodeChecker\EmptyCheck;
  31. use OC\App\CodeChecker\InfoChecker;
  32. use OC\App\CodeChecker\LanguageParseChecker;
  33. use OC\App\CodeChecker\PrivateCheck;
  34. use OC\App\CodeChecker\StrongComparisonCheck;
  35. use OC\App\InfoParser;
  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) {
  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. $this->analyseUpdateFile($appId, $output);
  140. if (empty($errors)) {
  141. $output->writeln('<info>App is compliant - awesome job!</info>');
  142. return 0;
  143. } else {
  144. $output->writeln('<error>App is not compliant</error>');
  145. return 101;
  146. }
  147. }
  148. /**
  149. * @param string $appId
  150. * @param $output
  151. */
  152. private function analyseUpdateFile($appId, OutputInterface $output) {
  153. $appPath = \OC_App::getAppPath($appId);
  154. if ($appPath === false) {
  155. throw new \RuntimeException("No app with given id <$appId> known.");
  156. }
  157. $updatePhp = $appPath . '/appinfo/update.php';
  158. if (file_exists($updatePhp)) {
  159. $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
  160. }
  161. }
  162. /**
  163. * @param string $optionName
  164. * @param CompletionContext $context
  165. * @return string[]
  166. */
  167. public function completeOptionValues($optionName, CompletionContext $context) {
  168. if ($optionName === 'checker') {
  169. return ['private', 'deprecation', 'strong-comparison'];
  170. }
  171. return [];
  172. }
  173. /**
  174. * @param string $argumentName
  175. * @param CompletionContext $context
  176. * @return string[]
  177. */
  178. public function completeArgumentValues($argumentName, CompletionContext $context) {
  179. if ($argumentName === 'app-id') {
  180. return \OC_App::getAllApps();
  181. }
  182. return [];
  183. }
  184. }