Completed
Push — master ( da9479...3a70eb )
by Morris
26:22 queued 12:23
created

CheckCode   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 217
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B configure() 0 29 1
D execute() 0 130 19
A analyseUpdateFile() 0 11 3
A completeOptionValues() 0 6 2
A completeArgumentValues() 0 6 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin McCorkell <[email protected]>
8
 * @author Thomas Müller <[email protected]>
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
26
namespace OC\Core\Command\App;
27
28
use OC\App\CodeChecker\CodeChecker;
29
use OC\App\CodeChecker\DatabaseSchemaChecker;
30
use OC\App\CodeChecker\EmptyCheck;
31
use OC\App\CodeChecker\InfoChecker;
32
use OC\App\CodeChecker\LanguageParseChecker;
33
use OC\App\InfoParser;
34
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
35
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
36
use Symfony\Component\Console\Command\Command;
37
use Symfony\Component\Console\Input\InputArgument;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Input\InputOption;
40
use Symfony\Component\Console\Output\OutputInterface;
41
42
class CheckCode extends Command implements CompletionAwareInterface  {
43
44
	/** @var InfoParser */
45
	private $infoParser;
46
47
	protected $checkers = [
48
		'private' => '\OC\App\CodeChecker\PrivateCheck',
49
		'deprecation' => '\OC\App\CodeChecker\DeprecationCheck',
50
		'strong-comparison' => '\OC\App\CodeChecker\StrongComparisonCheck',
51
	];
52
53
	public function __construct(InfoParser $infoParser) {
54
		parent::__construct();
55
		$this->infoParser = $infoParser;
56
	}
57
58
	protected function configure() {
59
		$this
60
			->setName('app:check-code')
61
			->setDescription('check code to be compliant')
62
			->addArgument(
63
				'app-id',
64
				InputArgument::REQUIRED,
65
				'check the specified app'
66
			)
67
			->addOption(
68
				'checker',
69
				'c',
70
				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
71
				'enable the specified checker(s)',
72
				[ 'private', 'deprecation', 'strong-comparison' ]
73
			)
74
			->addOption(
75
				'--skip-checkers',
76
				null,
77
				InputOption::VALUE_NONE,
78
				'skips the the code checkers to only check info.xml, language and database schema'
79
			)
80
			->addOption(
81
				'--skip-validate-info',
82
				null,
83
				InputOption::VALUE_NONE,
84
				'skips the info.xml/version check'
85
			);
86
	}
87
88
	protected function execute(InputInterface $input, OutputInterface $output) {
89
		$appId = $input->getArgument('app-id');
90
91
		$checkList = new EmptyCheck();
92
		foreach ($input->getOption('checker') as $checker) {
93
			if (!isset($this->checkers[$checker])) {
94
				throw new \InvalidArgumentException('Invalid checker: '.$checker);
95
			}
96
			$checkerClass = $this->checkers[$checker];
97
			$checkList = new $checkerClass($checkList);
98
		}
99
100
		$codeChecker = new CodeChecker($checkList);
101
102
		$codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) {
103
			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
104
				$output->writeln("<info>Analysing {$params}</info>");
105
			}
106
		});
107
		$codeChecker->listen('CodeChecker', 'analyseFileFinished', function($filename, $errors) use ($output) {
108
			$count = count($errors);
109
110
			// show filename if the verbosity is low, but there are errors in a file
111
			if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
112
				$output->writeln("<info>Analysing {$filename}</info>");
113
			}
114
115
			// show error count if there are errors present or the verbosity is high
116
			if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
117
				$output->writeln(" {$count} errors");
118
			}
119
			usort($errors, function($a, $b) {
120
				return $a['line'] >$b['line'];
121
			});
122
123
			foreach($errors as $p) {
124
				$line = sprintf("%' 4d", $p['line']);
125
				$output->writeln("    <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>");
126
			}
127
		});
128
		$errors = [];
129
		if(!$input->getOption('skip-checkers')) {
130
			$errors = $codeChecker->analyse($appId);
131
		}
132
133
		if(!$input->getOption('skip-validate-info')) {
134
			$infoChecker = new InfoChecker($this->infoParser);
135
136
			$infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
137
				$output->writeln("<error>Mandatory field missing: $key</error>");
138
			});
139
140
			$infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
141
				if($value === [] || is_null($value) || $value === '') {
142
					$output->writeln("<info>Deprecated field available: $key</info>");
143
				} else {
144
					$output->writeln("<info>Deprecated field available: $key => $value</info>");
145
				}
146
			});
147
148
			$infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) {
149
				$output->writeln("<comment>Nextcloud $minMax version requirement missing (will be an error in Nextcloud 12 and later)</comment>");
150
			});
151
152
			$infoChecker->listen('InfoChecker', 'duplicateRequirement', function($minMax) use ($output) {
153
				$output->writeln("<error>Duplicate $minMax ownCloud version requirement found</error>");
154
			});
155
156
			$infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) {
157
				$output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>");
158
			});
159
160
			$infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) {
161
				$output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>");
162
			});
163
164
			$infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) {
165
				$output->writeln("<info>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</info>");
166
			});
167
168
			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
169
				$infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
170
					$output->writeln("<info>Mandatory field available: $key => $value</info>");
171
				});
172
173
				$infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) {
174
					$output->writeln("<info>Optional field available: $key => $value</info>");
175
				});
176
177
				$infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) {
178
					$output->writeln("<info>Unused field available: $key => $value</info>");
179
				});
180
			}
181
182
			$infoErrors = $infoChecker->analyse($appId);
183
184
			$errors = array_merge($errors, $infoErrors);
185
186
			$languageParser = new LanguageParseChecker();
187
			$languageErrors = $languageParser->analyse($appId);
188
189
			foreach ($languageErrors as $languageError) {
190
				$output->writeln("<error>$languageError</error>");
191
			}
192
193
			$errors = array_merge($errors, $languageErrors);
194
195
			$databaseSchema = new DatabaseSchemaChecker();
196
			$schemaErrors = $databaseSchema->analyse($appId);
197
198
			foreach ($schemaErrors['errors'] as $schemaError) {
199
				$output->writeln("<error>$schemaError</error>");
200
			}
201
			foreach ($schemaErrors['warnings'] as $schemaWarning) {
202
				$output->writeln("<comment>$schemaWarning</comment>");
203
			}
204
205
			$errors = array_merge($errors, $schemaErrors['errors']);
206
		}
207
208
		$this->analyseUpdateFile($appId, $output);
209
210
		if (empty($errors)) {
211
			$output->writeln('<info>App is compliant - awesome job!</info>');
212
			return 0;
213
		} else {
214
			$output->writeln('<error>App is not compliant</error>');
215
			return 101;
216
		}
217
	}
218
219
	/**
220
	 * @param string $appId
221
	 * @param $output
222
	 */
223
	private function analyseUpdateFile($appId, OutputInterface $output) {
224
		$appPath = \OC_App::getAppPath($appId);
225
		if ($appPath === false) {
226
			throw new \RuntimeException("No app with given id <$appId> known.");
227
		}
228
229
		$updatePhp = $appPath . '/appinfo/update.php';
230
		if (file_exists($updatePhp)) {
231
			$output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
232
		}
233
	}
234
235
	/**
236
	 * @param string $optionName
237
	 * @param CompletionContext $context
238
	 * @return string[]
239
	 */
240
	public function completeOptionValues($optionName, CompletionContext $context) {
241
		if ($optionName === 'checker') {
242
			return ['private', 'deprecation', 'strong-comparison'];
243
		}
244
		return [];
245
	}
246
247
	/**
248
	 * @param string $argumentName
249
	 * @param CompletionContext $context
250
	 * @return string[]
251
	 */
252
	public function completeArgumentValues($argumentName, CompletionContext $context) {
253
		if ($argumentName === 'app-id') {
254
			return \OC_App::getAllApps();
255
		}
256
		return [];
257
	}
258
}
259