Completed
Push — master ( da9479...3a70eb )
by Morris
26:22 queued 12:23
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   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -41,218 +41,218 @@
 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 = [];
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
-	}
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 258
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 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,29 +108,29 @@  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 = [];
129
-		if(!$input->getOption('skip-checkers')) {
129
+		if (!$input->getOption('skip-checkers')) {
130 130
 			$errors = $codeChecker->analyse($appId);
131 131
 		}
132 132
 
133
-		if(!$input->getOption('skip-validate-info')) {
133
+		if (!$input->getOption('skip-validate-info')) {
134 134
 			$infoChecker = new InfoChecker($this->infoParser);
135 135
 
136 136
 			$infoChecker->listen('InfoChecker', 'mandatoryFieldMissing', function($key) use ($output) {
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 			});
139 139
 
140 140
 			$infoChecker->listen('InfoChecker', 'deprecatedFieldFound', function($key, $value) use ($output) {
141
-				if($value === [] || is_null($value) || $value === '') {
141
+				if ($value === [] || is_null($value) || $value === '') {
142 142
 					$output->writeln("<info>Deprecated field available: $key</info>");
143 143
 				} else {
144 144
 					$output->writeln("<info>Deprecated field available: $key => $value</info>");
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 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 166
 			});
167 167
 
168
-			if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
168
+			if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
169 169
 				$infoChecker->listen('InfoChecker', 'mandatoryFieldFound', function($key, $value) use ($output) {
170 170
 					$output->writeln("<info>Mandatory field available: $key => $value</info>");
171 171
 				});
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
 			throw new \RuntimeException("No app with given id <$appId> known.");
227 227
 		}
228 228
 
229
-		$updatePhp = $appPath . '/appinfo/update.php';
229
+		$updatePhp = $appPath.'/appinfo/update.php';
230 230
 		if (file_exists($updatePhp)) {
231 231
 			$output->writeln("<info>Deprecated file found: $updatePhp - please use repair steps</info>");
232 232
 		}
Please login to merge, or discard this patch.