You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CheckCode.php 6.6KB

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 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. // Can not inject because of circular dependency
  117. $infoChecker = new InfoChecker(\OC::$server->getAppManager());
  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. }