Completed
Pull Request — master (#4767)
by Joas
19:42
created
lib/private/App/CodeChecker/LanguageParseChecker.php 2 patches
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -25,36 +25,36 @@
 block discarded – undo
25 25
 
26 26
 class LanguageParseChecker {
27 27
 
28
-	/**
29
-	 * @param string $appId
30
-	 * @return array
31
-	 */
32
-	public function analyse($appId) {
33
-		$appPath = \OC_App::getAppPath($appId);
34
-		if ($appPath === false) {
35
-			throw new \RuntimeException("No app with given id <$appId> known.");
36
-		}
37
-
38
-		if (!is_dir($appPath . '/l10n/')) {
39
-			return [];
40
-		}
41
-
42
-		$errors = [];
43
-		$directory = new \DirectoryIterator($appPath . '/l10n/');
44
-
45
-		foreach ($directory as $file) {
46
-			if ($file->getExtension() !== 'json') {
47
-				continue;
48
-			}
49
-
50
-			$content = file_get_contents($file->getPathname());
51
-			json_decode($content, true);
52
-
53
-			if (json_last_error() !== JSON_ERROR_NONE) {
54
-				$errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg();
55
-			}
56
-		}
57
-
58
-		return $errors;
59
-	}
28
+    /**
29
+     * @param string $appId
30
+     * @return array
31
+     */
32
+    public function analyse($appId) {
33
+        $appPath = \OC_App::getAppPath($appId);
34
+        if ($appPath === false) {
35
+            throw new \RuntimeException("No app with given id <$appId> known.");
36
+        }
37
+
38
+        if (!is_dir($appPath . '/l10n/')) {
39
+            return [];
40
+        }
41
+
42
+        $errors = [];
43
+        $directory = new \DirectoryIterator($appPath . '/l10n/');
44
+
45
+        foreach ($directory as $file) {
46
+            if ($file->getExtension() !== 'json') {
47
+                continue;
48
+            }
49
+
50
+            $content = file_get_contents($file->getPathname());
51
+            json_decode($content, true);
52
+
53
+            if (json_last_error() !== JSON_ERROR_NONE) {
54
+                $errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg();
55
+            }
56
+        }
57
+
58
+        return $errors;
59
+    }
60 60
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -35,12 +35,12 @@  discard block
 block discarded – undo
35 35
 			throw new \RuntimeException("No app with given id <$appId> known.");
36 36
 		}
37 37
 
38
-		if (!is_dir($appPath . '/l10n/')) {
38
+		if (!is_dir($appPath.'/l10n/')) {
39 39
 			return [];
40 40
 		}
41 41
 
42 42
 		$errors = [];
43
-		$directory = new \DirectoryIterator($appPath . '/l10n/');
43
+		$directory = new \DirectoryIterator($appPath.'/l10n/');
44 44
 
45 45
 		foreach ($directory as $file) {
46 46
 			if ($file->getExtension() !== 'json') {
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 			json_decode($content, true);
52 52
 
53 53
 			if (json_last_error() !== JSON_ERROR_NONE) {
54
-				$errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg();
54
+				$errors[] = 'Invalid language file found: l10n/'.$file->getFilename().': '.json_last_error_msg();
55 55
 			}
56 56
 		}
57 57
 
Please login to merge, or discard this patch.
lib/private/App/CodeChecker/DatabaseSchemaChecker.php 2 patches
Indentation   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -25,81 +25,81 @@
 block discarded – undo
25 25
 
26 26
 class DatabaseSchemaChecker {
27 27
 
28
-	/**
29
-	 * @param string $appId
30
-	 * @return array
31
-	 */
32
-	public function analyse($appId) {
33
-		$appPath = \OC_App::getAppPath($appId);
34
-		if ($appPath === false) {
35
-			throw new \RuntimeException("No app with given id <$appId> known.");
36
-		}
37
-
38
-		if (!file_exists($appPath . '/appinfo/database.xml')) {
39
-			return ['errors' => [], 'warnings' => []];
40
-		}
41
-
42
-		libxml_use_internal_errors(true);
43
-		$loadEntities = libxml_disable_entity_loader(false);
44
-		$xml = simplexml_load_file($appPath . '/appinfo/database.xml');
45
-		libxml_disable_entity_loader($loadEntities);
46
-
47
-
48
-		$errors = $warnings = [];
49
-
50
-		foreach ($xml->table as $table) {
51
-			// Table names
52
-			if (strpos($table->name, '*dbprefix*') !== 0) {
53
-				$errors[] = 'Database schema error: name of table ' . $table->name . ' does not start with *dbprefix*';
54
-			}
55
-			$tableName = substr($table->name, strlen('*dbprefix*'));
56
-			if (strpos($tableName, '*dbprefix*') !== false) {
57
-				$warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . $table->name;
58
-			}
59
-
60
-			if (strlen($tableName) > 27) {
61
-				$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
62
-			}
63
-
64
-			$hasAutoIncrement = false;
65
-
66
-			// Column names
67
-			foreach ($table->declaration->field as $column) {
68
-				if (strpos($column->name, '*dbprefix*') !== false) {
69
-					$warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . $column->name . ' on table ' . $table->name;
70
-				}
71
-
72
-				if (strlen($column->name) > 30) {
73
-					$errors[] = 'Database schema error: Name of column ' . $column->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed';
74
-				}
75
-
76
-				if ($column->autoincrement) {
77
-					if ($hasAutoIncrement) {
78
-						$errors[] = 'Database schema error: Table ' . $table->name . ' has multiple autoincrement columns';
79
-					}
80
-
81
-					if (strlen($tableName) > 21) {
82
-						$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
83
-					}
84
-
85
-					$hasAutoIncrement = true;
86
-				}
87
-			}
88
-
89
-			// Index names
90
-			foreach ($table->declaration->index as $index) {
91
-				$hasPrefix = strpos($index->name, '*dbprefix*');
92
-				if ($hasPrefix !== false && $hasPrefix !== 0) {
93
-					$warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . $index->name . ' on table ' . $table->name;
94
-				}
95
-
96
-				$indexName = $hasPrefix === 0 ? substr($index->name, strlen('*dbprefix*')) : $index->name;
97
-				if (strlen($indexName) > 27) {
98
-					$errors[] = 'Database schema error: Name of index ' . $index->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed';
99
-				}
100
-			}
101
-		}
102
-
103
-		return ['errors' => $errors, 'warnings' => $warnings];
104
-	}
28
+    /**
29
+     * @param string $appId
30
+     * @return array
31
+     */
32
+    public function analyse($appId) {
33
+        $appPath = \OC_App::getAppPath($appId);
34
+        if ($appPath === false) {
35
+            throw new \RuntimeException("No app with given id <$appId> known.");
36
+        }
37
+
38
+        if (!file_exists($appPath . '/appinfo/database.xml')) {
39
+            return ['errors' => [], 'warnings' => []];
40
+        }
41
+
42
+        libxml_use_internal_errors(true);
43
+        $loadEntities = libxml_disable_entity_loader(false);
44
+        $xml = simplexml_load_file($appPath . '/appinfo/database.xml');
45
+        libxml_disable_entity_loader($loadEntities);
46
+
47
+
48
+        $errors = $warnings = [];
49
+
50
+        foreach ($xml->table as $table) {
51
+            // Table names
52
+            if (strpos($table->name, '*dbprefix*') !== 0) {
53
+                $errors[] = 'Database schema error: name of table ' . $table->name . ' does not start with *dbprefix*';
54
+            }
55
+            $tableName = substr($table->name, strlen('*dbprefix*'));
56
+            if (strpos($tableName, '*dbprefix*') !== false) {
57
+                $warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . $table->name;
58
+            }
59
+
60
+            if (strlen($tableName) > 27) {
61
+                $errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
62
+            }
63
+
64
+            $hasAutoIncrement = false;
65
+
66
+            // Column names
67
+            foreach ($table->declaration->field as $column) {
68
+                if (strpos($column->name, '*dbprefix*') !== false) {
69
+                    $warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . $column->name . ' on table ' . $table->name;
70
+                }
71
+
72
+                if (strlen($column->name) > 30) {
73
+                    $errors[] = 'Database schema error: Name of column ' . $column->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed';
74
+                }
75
+
76
+                if ($column->autoincrement) {
77
+                    if ($hasAutoIncrement) {
78
+                        $errors[] = 'Database schema error: Table ' . $table->name . ' has multiple autoincrement columns';
79
+                    }
80
+
81
+                    if (strlen($tableName) > 21) {
82
+                        $errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
83
+                    }
84
+
85
+                    $hasAutoIncrement = true;
86
+                }
87
+            }
88
+
89
+            // Index names
90
+            foreach ($table->declaration->index as $index) {
91
+                $hasPrefix = strpos($index->name, '*dbprefix*');
92
+                if ($hasPrefix !== false && $hasPrefix !== 0) {
93
+                    $warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . $index->name . ' on table ' . $table->name;
94
+                }
95
+
96
+                $indexName = $hasPrefix === 0 ? substr($index->name, strlen('*dbprefix*')) : $index->name;
97
+                if (strlen($indexName) > 27) {
98
+                    $errors[] = 'Database schema error: Name of index ' . $index->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed';
99
+                }
100
+            }
101
+        }
102
+
103
+        return ['errors' => $errors, 'warnings' => $warnings];
104
+    }
105 105
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -35,13 +35,13 @@  discard block
 block discarded – undo
35 35
 			throw new \RuntimeException("No app with given id <$appId> known.");
36 36
 		}
37 37
 
38
-		if (!file_exists($appPath . '/appinfo/database.xml')) {
38
+		if (!file_exists($appPath.'/appinfo/database.xml')) {
39 39
 			return ['errors' => [], 'warnings' => []];
40 40
 		}
41 41
 
42 42
 		libxml_use_internal_errors(true);
43 43
 		$loadEntities = libxml_disable_entity_loader(false);
44
-		$xml = simplexml_load_file($appPath . '/appinfo/database.xml');
44
+		$xml = simplexml_load_file($appPath.'/appinfo/database.xml');
45 45
 		libxml_disable_entity_loader($loadEntities);
46 46
 
47 47
 
@@ -50,15 +50,15 @@  discard block
 block discarded – undo
50 50
 		foreach ($xml->table as $table) {
51 51
 			// Table names
52 52
 			if (strpos($table->name, '*dbprefix*') !== 0) {
53
-				$errors[] = 'Database schema error: name of table ' . $table->name . ' does not start with *dbprefix*';
53
+				$errors[] = 'Database schema error: name of table '.$table->name.' does not start with *dbprefix*';
54 54
 			}
55 55
 			$tableName = substr($table->name, strlen('*dbprefix*'));
56 56
 			if (strpos($tableName, '*dbprefix*') !== false) {
57
-				$warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . $table->name;
57
+				$warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table '.$table->name;
58 58
 			}
59 59
 
60 60
 			if (strlen($tableName) > 27) {
61
-				$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
61
+				$errors[] = 'Database schema error: Name of table '.$table->name.' is too long ('.strlen($tableName).'), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
62 62
 			}
63 63
 
64 64
 			$hasAutoIncrement = false;
@@ -66,20 +66,20 @@  discard block
 block discarded – undo
66 66
 			// Column names
67 67
 			foreach ($table->declaration->field as $column) {
68 68
 				if (strpos($column->name, '*dbprefix*') !== false) {
69
-					$warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . $column->name . ' on table ' . $table->name;
69
+					$warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column '.$column->name.' on table '.$table->name;
70 70
 				}
71 71
 
72 72
 				if (strlen($column->name) > 30) {
73
-					$errors[] = 'Database schema error: Name of column ' . $column->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed';
73
+					$errors[] = 'Database schema error: Name of column '.$column->name.' on table '.$table->name.' is too long ('.strlen($tableName).'), max. 30 characters allowed';
74 74
 				}
75 75
 
76 76
 				if ($column->autoincrement) {
77 77
 					if ($hasAutoIncrement) {
78
-						$errors[] = 'Database schema error: Table ' . $table->name . ' has multiple autoincrement columns';
78
+						$errors[] = 'Database schema error: Table '.$table->name.' has multiple autoincrement columns';
79 79
 					}
80 80
 
81 81
 					if (strlen($tableName) > 21) {
82
-						$errors[] = 'Database schema error: Name of table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
82
+						$errors[] = 'Database schema error: Name of table '.$table->name.' is too long ('.strlen($tableName).'), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
83 83
 					}
84 84
 
85 85
 					$hasAutoIncrement = true;
@@ -90,12 +90,12 @@  discard block
 block discarded – undo
90 90
 			foreach ($table->declaration->index as $index) {
91 91
 				$hasPrefix = strpos($index->name, '*dbprefix*');
92 92
 				if ($hasPrefix !== false && $hasPrefix !== 0) {
93
-					$warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . $index->name . ' on table ' . $table->name;
93
+					$warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index '.$index->name.' on table '.$table->name;
94 94
 				}
95 95
 
96 96
 				$indexName = $hasPrefix === 0 ? substr($index->name, strlen('*dbprefix*')) : $index->name;
97 97
 				if (strlen($indexName) > 27) {
98
-					$errors[] = 'Database schema error: Name of index ' . $index->name . ' on table ' . $table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed';
98
+					$errors[] = 'Database schema error: Name of index '.$index->name.' on table '.$table->name.' is too long ('.strlen($tableName).'), max. 27 characters + *dbprefix* allowed';
99 99
 				}
100 100
 			}
101 101
 		}
Please login to merge, or discard this patch.
core/Command/App/CheckCode.php 2 patches
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -41,215 +41,215 @@
 block discarded – undo
41 41
 
42 42
 class CheckCode extends Command implements CompletionAwareInterface  {
43 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 = $codeChecker->analyse($appId);
129
-
130
-		if(!$input->getOption('skip-validate-info')) {
131
-			$infoChecker = new InfoChecker($this->infoParser);
132
-
133
-			$infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
134
-				$output->writeln("<error>Mandatory field missing: $key</error>");
135
-			});
136
-
137
-			$infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
138
-				if($value === [] || is_null($value) || $value === '') {
139
-					$output->writeln("<info>Deprecated field available: $key</info>");
140
-				} else {
141
-					$output->writeln("<info>Deprecated field available: $key => $value</info>");
142
-				}
143
-			});
144
-
145
-			$infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) {
146
-				$output->writeln("<comment>Nextcloud $minMax version requirement missing (will be an error in Nextcloud 12 and later)</comment>");
147
-			});
148
-
149
-			$infoChecker->listen('InfoChecker', 'duplicateRequirement', function($minMax) use ($output) {
150
-				$output->writeln("<error>Duplicate $minMax ownCloud version requirement found</error>");
151
-			});
152
-
153
-			$infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) {
154
-				$output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>");
155
-			});
156
-
157
-			$infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) {
158
-				$output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>");
159
-			});
160
-
161
-			$infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) {
162
-				$output->writeln("<info>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</info>");
163
-			});
164
-
165
-			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
166
-				$infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
167
-					$output->writeln("<info>Mandatory field available: $key => $value</info>");
168
-				});
169
-
170
-				$infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) {
171
-					$output->writeln("<info>Optional field available: $key => $value</info>");
172
-				});
173
-
174
-				$infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) {
175
-					$output->writeln("<info>Unused field available: $key => $value</info>");
176
-				});
177
-			}
178
-
179
-			$infoErrors = $infoChecker->analyse($appId);
180
-
181
-			$errors = array_merge($errors, $infoErrors);
182
-
183
-			$languageParser = new LanguageParseChecker();
184
-			$languageErrors = $languageParser->analyse($appId);
185
-
186
-			foreach ($languageErrors as $languageError) {
187
-				$output->writeln("<error>$languageError</error>");
188
-			}
189
-
190
-			$errors = array_merge($errors, $languageErrors);
191
-
192
-			$databaseSchema = new DatabaseSchemaChecker();
193
-			$schemaErrors = $databaseSchema->analyse($appId);
194
-
195
-			foreach ($schemaErrors['errors'] as $schemaError) {
196
-				$output->writeln("<error>$schemaError</error>");
197
-			}
198
-			foreach ($schemaErrors['warnings'] as $schemaWarning) {
199
-				$output->writeln("<comment>$schemaWarning</comment>");
200
-			}
201
-
202
-			$errors = array_merge($errors, $schemaErrors['errors']);
203
-		}
204
-
205
-		$this->analyseUpdateFile($appId, $output);
206
-
207
-		if (empty($errors)) {
208
-			$output->writeln('<info>App is compliant - awesome job!</info>');
209
-			return 0;
210
-		} else {
211
-			$output->writeln('<error>App is not compliant</error>');
212
-			return 101;
213
-		}
214
-	}
215
-
216
-	/**
217
-	 * @param string $appId
218
-	 * @param $output
219
-	 */
220
-	private function analyseUpdateFile($appId, OutputInterface $output) {
221
-		$appPath = \OC_App::getAppPath($appId);
222
-		if ($appPath === false) {
223
-			throw new \RuntimeException("No app with given id <$appId> known.");
224
-		}
225
-
226
-		$updatePhp = $appPath . '/appinfo/update.php';
227
-		if (file_exists($updatePhp)) {
228
-			$output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
229
-		}
230
-	}
231
-
232
-	/**
233
-	 * @param string $optionName
234
-	 * @param CompletionContext $context
235
-	 * @return string[]
236
-	 */
237
-	public function completeOptionValues($optionName, CompletionContext $context) {
238
-		if ($optionName === 'checker') {
239
-			return ['private', 'deprecation', 'strong-comparison'];
240
-		}
241
-		return [];
242
-	}
243
-
244
-	/**
245
-	 * @param string $argumentName
246
-	 * @param CompletionContext $context
247
-	 * @return string[]
248
-	 */
249
-	public function completeArgumentValues($argumentName, CompletionContext $context) {
250
-		if ($argumentName === 'app-id') {
251
-			return \OC_App::getAllApps();
252
-		}
253
-		return [];
254
-	}
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 = $codeChecker->analyse($appId);
129
+
130
+        if(!$input->getOption('skip-validate-info')) {
131
+            $infoChecker = new InfoChecker($this->infoParser);
132
+
133
+            $infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
134
+                $output->writeln("<error>Mandatory field missing: $key</error>");
135
+            });
136
+
137
+            $infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
138
+                if($value === [] || is_null($value) || $value === '') {
139
+                    $output->writeln("<info>Deprecated field available: $key</info>");
140
+                } else {
141
+                    $output->writeln("<info>Deprecated field available: $key => $value</info>");
142
+                }
143
+            });
144
+
145
+            $infoChecker->listen('InfoChecker', 'missingRequirement', function($minMax) use ($output) {
146
+                $output->writeln("<comment>Nextcloud $minMax version requirement missing (will be an error in Nextcloud 12 and later)</comment>");
147
+            });
148
+
149
+            $infoChecker->listen('InfoChecker', 'duplicateRequirement', function($minMax) use ($output) {
150
+                $output->writeln("<error>Duplicate $minMax ownCloud version requirement found</error>");
151
+            });
152
+
153
+            $infoChecker->listen('InfoChecker', 'differentVersions', function($versionFile, $infoXML) use ($output) {
154
+                $output->writeln("<error>Different versions provided (appinfo/version: $versionFile - appinfo/info.xml: $infoXML)</error>");
155
+            });
156
+
157
+            $infoChecker->listen('InfoChecker', 'sameVersions', function($path) use ($output) {
158
+                $output->writeln("<info>Version file isn't needed anymore and can be safely removed ($path)</info>");
159
+            });
160
+
161
+            $infoChecker->listen('InfoChecker', 'migrateVersion', function($version) use ($output) {
162
+                $output->writeln("<info>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</info>");
163
+            });
164
+
165
+            if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
166
+                $infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
167
+                    $output->writeln("<info>Mandatory field available: $key => $value</info>");
168
+                });
169
+
170
+                $infoChecker->listen('InfoChecker', 'optionalFieldFound', function($key, $value) use ($output) {
171
+                    $output->writeln("<info>Optional field available: $key => $value</info>");
172
+                });
173
+
174
+                $infoChecker->listen('InfoChecker', 'unusedFieldFound', function($key, $value) use ($output) {
175
+                    $output->writeln("<info>Unused field available: $key => $value</info>");
176
+                });
177
+            }
178
+
179
+            $infoErrors = $infoChecker->analyse($appId);
180
+
181
+            $errors = array_merge($errors, $infoErrors);
182
+
183
+            $languageParser = new LanguageParseChecker();
184
+            $languageErrors = $languageParser->analyse($appId);
185
+
186
+            foreach ($languageErrors as $languageError) {
187
+                $output->writeln("<error>$languageError</error>");
188
+            }
189
+
190
+            $errors = array_merge($errors, $languageErrors);
191
+
192
+            $databaseSchema = new DatabaseSchemaChecker();
193
+            $schemaErrors = $databaseSchema->analyse($appId);
194
+
195
+            foreach ($schemaErrors['errors'] as $schemaError) {
196
+                $output->writeln("<error>$schemaError</error>");
197
+            }
198
+            foreach ($schemaErrors['warnings'] as $schemaWarning) {
199
+                $output->writeln("<comment>$schemaWarning</comment>");
200
+            }
201
+
202
+            $errors = array_merge($errors, $schemaErrors['errors']);
203
+        }
204
+
205
+        $this->analyseUpdateFile($appId, $output);
206
+
207
+        if (empty($errors)) {
208
+            $output->writeln('<info>App is compliant - awesome job!</info>');
209
+            return 0;
210
+        } else {
211
+            $output->writeln('<error>App is not compliant</error>');
212
+            return 101;
213
+        }
214
+    }
215
+
216
+    /**
217
+     * @param string $appId
218
+     * @param $output
219
+     */
220
+    private function analyseUpdateFile($appId, OutputInterface $output) {
221
+        $appPath = \OC_App::getAppPath($appId);
222
+        if ($appPath === false) {
223
+            throw new \RuntimeException("No app with given id <$appId> known.");
224
+        }
225
+
226
+        $updatePhp = $appPath . '/appinfo/update.php';
227
+        if (file_exists($updatePhp)) {
228
+            $output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
229
+        }
230
+    }
231
+
232
+    /**
233
+     * @param string $optionName
234
+     * @param CompletionContext $context
235
+     * @return string[]
236
+     */
237
+    public function completeOptionValues($optionName, CompletionContext $context) {
238
+        if ($optionName === 'checker') {
239
+            return ['private', 'deprecation', 'strong-comparison'];
240
+        }
241
+        return [];
242
+    }
243
+
244
+    /**
245
+     * @param string $argumentName
246
+     * @param CompletionContext $context
247
+     * @return string[]
248
+     */
249
+    public function completeArgumentValues($argumentName, CompletionContext $context) {
250
+        if ($argumentName === 'app-id') {
251
+            return \OC_App::getAllApps();
252
+        }
253
+        return [];
254
+    }
255 255
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 use Symfony\Component\Console\Input\InputOption;
40 40
 use Symfony\Component\Console\Output\OutputInterface;
41 41
 
42
-class CheckCode extends Command implements CompletionAwareInterface  {
42
+class CheckCode extends Command implements CompletionAwareInterface {
43 43
 
44 44
 	/** @var InfoParser */
45 45
 	private $infoParser;
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 				'c',
70 70
 				InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
71 71
 				'enable the specified checker(s)',
72
-				[ 'private', 'deprecation', 'strong-comparison' ]
72
+				['private', 'deprecation', 'strong-comparison']
73 73
 			)
74 74
 			->addOption(
75 75
 				'--skip-checkers',
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 		$codeChecker = new CodeChecker($checkList);
101 101
 
102 102
 		$codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) {
103
-			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
103
+			if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
104 104
 				$output->writeln("<info>Analysing {$params}</info>");
105 105
 			}
106 106
 		});
@@ -108,26 +108,26 @@  discard block
 block discarded – undo
108 108
 			$count = count($errors);
109 109
 
110 110
 			// show filename if the verbosity is low, but there are errors in a file
111
-			if($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
111
+			if ($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
112 112
 				$output->writeln("<info>Analysing {$filename}</info>");
113 113
 			}
114 114
 
115 115
 			// show error count if there are errors present or the verbosity is high
116
-			if($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
116
+			if ($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
117 117
 				$output->writeln(" {$count} errors");
118 118
 			}
119 119
 			usort($errors, function($a, $b) {
120
-				return $a['line'] >$b['line'];
120
+				return $a['line'] > $b['line'];
121 121
 			});
122 122
 
123
-			foreach($errors as $p) {
123
+			foreach ($errors as $p) {
124 124
 				$line = sprintf("%' 4d", $p['line']);
125 125
 				$output->writeln("    <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>");
126 126
 			}
127 127
 		});
128 128
 		$errors = $codeChecker->analyse($appId);
129 129
 
130
-		if(!$input->getOption('skip-validate-info')) {
130
+		if (!$input->getOption('skip-validate-info')) {
131 131
 			$infoChecker = new InfoChecker($this->infoParser);
132 132
 
133 133
 			$infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 			});
136 136
 
137 137
 			$infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
138
-				if($value === [] || is_null($value) || $value === '') {
138
+				if ($value === [] || is_null($value) || $value === '') {
139 139
 					$output->writeln("<info>Deprecated field available: $key</info>");
140 140
 				} else {
141 141
 					$output->writeln("<info>Deprecated field available: $key => $value</info>");
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 				$output->writeln("<info>Migrate the app version to appinfo/info.xml (add <version>$version</version> to appinfo/info.xml and remove appinfo/version)</info>");
163 163
 			});
164 164
 
165
-			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
165
+			if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
166 166
 				$infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
167 167
 					$output->writeln("<info>Mandatory field available: $key => $value</info>");
168 168
 				});
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
 			throw new \RuntimeException("No app with given id <$appId> known.");
224 224
 		}
225 225
 
226
-		$updatePhp = $appPath . '/appinfo/update.php';
226
+		$updatePhp = $appPath.'/appinfo/update.php';
227 227
 		if (file_exists($updatePhp)) {
228 228
 			$output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
229 229
 		}
Please login to merge, or discard this patch.