Make the app code checker a NOOPtags/v22.0.0beta1
@@ -27,28 +27,14 @@ | |||
namespace OC\Core\Command\App; | |||
use OC\App\CodeChecker\CodeChecker; | |||
use OC\App\CodeChecker\DatabaseSchemaChecker; | |||
use OC\App\CodeChecker\DeprecationCheck; | |||
use OC\App\CodeChecker\EmptyCheck; | |||
use OC\App\CodeChecker\InfoChecker; | |||
use OC\App\CodeChecker\LanguageParseChecker; | |||
use OC\App\CodeChecker\PrivateCheck; | |||
use OC\App\CodeChecker\StrongComparisonCheck; | |||
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; | |||
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; | |||
use Symfony\Component\Console\Command\Command; | |||
use Symfony\Component\Console\Input\InputArgument; | |||
use Symfony\Component\Console\Input\InputInterface; | |||
use Symfony\Component\Console\Input\InputOption; | |||
use Symfony\Component\Console\Output\OutputInterface; | |||
class CheckCode extends Command implements CompletionAwareInterface { | |||
protected $checkers = [ | |||
'private' => PrivateCheck::class, | |||
'deprecation' => DeprecationCheck::class, | |||
'strong-comparison' => StrongComparisonCheck::class, | |||
]; | |||
class CheckCode extends Command { | |||
protected $checkers = []; | |||
protected function configure() { | |||
$this | |||
@@ -81,112 +67,8 @@ class CheckCode extends Command implements CompletionAwareInterface { | |||
} | |||
protected function execute(InputInterface $input, OutputInterface $output): int { | |||
$appId = $input->getArgument('app-id'); | |||
$output->writeln('<error>The app code checker doesn\t check anything and this command will be removed in Nextcloud 23</error>'); | |||
$checkList = new EmptyCheck(); | |||
foreach ($input->getOption('checker') as $checker) { | |||
if (!isset($this->checkers[$checker])) { | |||
throw new \InvalidArgumentException('Invalid checker: '.$checker); | |||
} | |||
$checkerClass = $this->checkers[$checker]; | |||
$checkList = new $checkerClass($checkList); | |||
} | |||
$codeChecker = new CodeChecker($checkList, !$input->getOption('skip-validate-info')); | |||
$codeChecker->listen('CodeChecker', 'analyseFileBegin', function ($params) use ($output) { | |||
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { | |||
$output->writeln("<info>Analysing {$params}</info>"); | |||
} | |||
}); | |||
$codeChecker->listen('CodeChecker', 'analyseFileFinished', function ($filename, $errors) use ($output) { | |||
$count = count($errors); | |||
// show filename if the verbosity is low, but there are errors in a file | |||
if ($count > 0 && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) { | |||
$output->writeln("<info>Analysing {$filename}</info>"); | |||
} | |||
// show error count if there are errors present or the verbosity is high | |||
if ($count > 0 || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { | |||
$output->writeln(" {$count} errors"); | |||
} | |||
usort($errors, function ($a, $b) { | |||
return $a['line'] > $b['line']; | |||
}); | |||
foreach ($errors as $p) { | |||
$line = sprintf("%' 4d", $p['line']); | |||
$output->writeln(" <error>line $line: {$p['disallowedToken']} - {$p['reason']}</error>"); | |||
} | |||
}); | |||
$errors = []; | |||
if (!$input->getOption('skip-checkers')) { | |||
$errors = $codeChecker->analyse($appId); | |||
} | |||
if (!$input->getOption('skip-validate-info')) { | |||
$infoChecker = new InfoChecker(); | |||
$infoChecker->listen('InfoChecker', 'parseError', function ($error) use ($output) { | |||
$output->writeln("<error>Invalid appinfo.xml file found: $error</error>"); | |||
}); | |||
$infoErrors = $infoChecker->analyse($appId); | |||
$errors = array_merge($errors, $infoErrors); | |||
$languageParser = new LanguageParseChecker(); | |||
$languageErrors = $languageParser->analyse($appId); | |||
foreach ($languageErrors as $languageError) { | |||
$output->writeln("<error>$languageError</error>"); | |||
} | |||
$errors = array_merge($errors, $languageErrors); | |||
$databaseSchema = new DatabaseSchemaChecker(); | |||
$schemaErrors = $databaseSchema->analyse($appId); | |||
foreach ($schemaErrors['errors'] as $schemaError) { | |||
$output->writeln("<error>$schemaError</error>"); | |||
} | |||
foreach ($schemaErrors['warnings'] as $schemaWarning) { | |||
$output->writeln("<comment>$schemaWarning</comment>"); | |||
} | |||
$errors = array_merge($errors, $schemaErrors['errors']); | |||
} | |||
if (empty($errors)) { | |||
$output->writeln('<info>App is compliant - awesome job!</info>'); | |||
return 0; | |||
} else { | |||
$output->writeln('<error>App is not compliant</error>'); | |||
return 101; | |||
} | |||
} | |||
/** | |||
* @param string $optionName | |||
* @param CompletionContext $context | |||
* @return string[] | |||
*/ | |||
public function completeOptionValues($optionName, CompletionContext $context) { | |||
if ($optionName === 'checker') { | |||
return ['private', 'deprecation', 'strong-comparison']; | |||
} | |||
return []; | |||
} | |||
/** | |||
* @param string $argumentName | |||
* @param CompletionContext $context | |||
* @return string[] | |||
*/ | |||
public function completeArgumentValues($argumentName, CompletionContext $context) { | |||
if ($argumentName === 'app-id') { | |||
return \OC_App::getAllApps(); | |||
} | |||
return []; | |||
return 0; | |||
} | |||
} |
@@ -652,18 +652,6 @@ return array( | |||
'OC\\App\\AppStore\\Fetcher\\Fetcher' => $baseDir . '/lib/private/App/AppStore/Fetcher/Fetcher.php', | |||
'OC\\App\\AppStore\\Version\\Version' => $baseDir . '/lib/private/App/AppStore/Version/Version.php', | |||
'OC\\App\\AppStore\\Version\\VersionParser' => $baseDir . '/lib/private/App/AppStore/Version/VersionParser.php', | |||
'OC\\App\\CodeChecker\\AbstractCheck' => $baseDir . '/lib/private/App/CodeChecker/AbstractCheck.php', | |||
'OC\\App\\CodeChecker\\CodeChecker' => $baseDir . '/lib/private/App/CodeChecker/CodeChecker.php', | |||
'OC\\App\\CodeChecker\\DatabaseSchemaChecker' => $baseDir . '/lib/private/App/CodeChecker/DatabaseSchemaChecker.php', | |||
'OC\\App\\CodeChecker\\DeprecationCheck' => $baseDir . '/lib/private/App/CodeChecker/DeprecationCheck.php', | |||
'OC\\App\\CodeChecker\\EmptyCheck' => $baseDir . '/lib/private/App/CodeChecker/EmptyCheck.php', | |||
'OC\\App\\CodeChecker\\ICheck' => $baseDir . '/lib/private/App/CodeChecker/ICheck.php', | |||
'OC\\App\\CodeChecker\\InfoChecker' => $baseDir . '/lib/private/App/CodeChecker/InfoChecker.php', | |||
'OC\\App\\CodeChecker\\LanguageParseChecker' => $baseDir . '/lib/private/App/CodeChecker/LanguageParseChecker.php', | |||
'OC\\App\\CodeChecker\\MigrationSchemaChecker' => $baseDir . '/lib/private/App/CodeChecker/MigrationSchemaChecker.php', | |||
'OC\\App\\CodeChecker\\NodeVisitor' => $baseDir . '/lib/private/App/CodeChecker/NodeVisitor.php', | |||
'OC\\App\\CodeChecker\\PrivateCheck' => $baseDir . '/lib/private/App/CodeChecker/PrivateCheck.php', | |||
'OC\\App\\CodeChecker\\StrongComparisonCheck' => $baseDir . '/lib/private/App/CodeChecker/StrongComparisonCheck.php', | |||
'OC\\App\\CompareVersion' => $baseDir . '/lib/private/App/CompareVersion.php', | |||
'OC\\App\\DependencyAnalyzer' => $baseDir . '/lib/private/App/DependencyAnalyzer.php', | |||
'OC\\App\\InfoParser' => $baseDir . '/lib/private/App/InfoParser.php', |
@@ -681,18 +681,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c | |||
'OC\\App\\AppStore\\Fetcher\\Fetcher' => __DIR__ . '/../../..' . '/lib/private/App/AppStore/Fetcher/Fetcher.php', | |||
'OC\\App\\AppStore\\Version\\Version' => __DIR__ . '/../../..' . '/lib/private/App/AppStore/Version/Version.php', | |||
'OC\\App\\AppStore\\Version\\VersionParser' => __DIR__ . '/../../..' . '/lib/private/App/AppStore/Version/VersionParser.php', | |||
'OC\\App\\CodeChecker\\AbstractCheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/AbstractCheck.php', | |||
'OC\\App\\CodeChecker\\CodeChecker' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/CodeChecker.php', | |||
'OC\\App\\CodeChecker\\DatabaseSchemaChecker' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/DatabaseSchemaChecker.php', | |||
'OC\\App\\CodeChecker\\DeprecationCheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/DeprecationCheck.php', | |||
'OC\\App\\CodeChecker\\EmptyCheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/EmptyCheck.php', | |||
'OC\\App\\CodeChecker\\ICheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/ICheck.php', | |||
'OC\\App\\CodeChecker\\InfoChecker' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/InfoChecker.php', | |||
'OC\\App\\CodeChecker\\LanguageParseChecker' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/LanguageParseChecker.php', | |||
'OC\\App\\CodeChecker\\MigrationSchemaChecker' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/MigrationSchemaChecker.php', | |||
'OC\\App\\CodeChecker\\NodeVisitor' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/NodeVisitor.php', | |||
'OC\\App\\CodeChecker\\PrivateCheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/PrivateCheck.php', | |||
'OC\\App\\CodeChecker\\StrongComparisonCheck' => __DIR__ . '/../../..' . '/lib/private/App/CodeChecker/StrongComparisonCheck.php', | |||
'OC\\App\\CompareVersion' => __DIR__ . '/../../..' . '/lib/private/App/CompareVersion.php', | |||
'OC\\App\\DependencyAnalyzer' => __DIR__ . '/../../..' . '/lib/private/App/DependencyAnalyzer.php', | |||
'OC\\App\\InfoParser' => __DIR__ . '/../../..' . '/lib/private/App/InfoParser.php', |
@@ -1,140 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
abstract class AbstractCheck implements ICheck { | |||
/** @var ICheck */ | |||
protected $check; | |||
/** | |||
* @param ICheck $check | |||
*/ | |||
public function __construct(ICheck $check) { | |||
$this->check = $check; | |||
} | |||
/** | |||
* @param int $errorCode | |||
* @param string $errorObject | |||
* @return string | |||
*/ | |||
public function getDescription($errorCode, $errorObject) { | |||
switch ($errorCode) { | |||
case CodeChecker::STATIC_CALL_NOT_ALLOWED: | |||
$functions = $this->getLocalFunctions(); | |||
$functions = array_change_key_case($functions, CASE_LOWER); | |||
if (isset($functions[$errorObject])) { | |||
return $this->getLocalDescription(); | |||
} | |||
// no break; | |||
case CodeChecker::CLASS_EXTENDS_NOT_ALLOWED: | |||
case CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED: | |||
case CodeChecker::CLASS_NEW_NOT_ALLOWED: | |||
case CodeChecker::CLASS_USE_NOT_ALLOWED: | |||
$classes = $this->getLocalClasses(); | |||
$classes = array_change_key_case($classes, CASE_LOWER); | |||
if (isset($classes[$errorObject])) { | |||
return $this->getLocalDescription(); | |||
} | |||
break; | |||
case CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED: | |||
$constants = $this->getLocalConstants(); | |||
$constants = array_change_key_case($constants, CASE_LOWER); | |||
if (isset($constants[$errorObject])) { | |||
return $this->getLocalDescription(); | |||
} | |||
break; | |||
case CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED: | |||
$methods = $this->getLocalMethods(); | |||
$methods = array_change_key_case($methods, CASE_LOWER); | |||
if (isset($methods[$errorObject])) { | |||
return $this->getLocalDescription(); | |||
} | |||
break; | |||
} | |||
return $this->check->getDescription($errorCode, $errorObject); | |||
} | |||
/** | |||
* @return string | |||
*/ | |||
abstract protected function getLocalDescription(); | |||
/** | |||
* @return array | |||
*/ | |||
abstract protected function getLocalClasses(); | |||
/** | |||
* @return array | |||
*/ | |||
abstract protected function getLocalConstants(); | |||
/** | |||
* @return array | |||
*/ | |||
abstract protected function getLocalFunctions(); | |||
/** | |||
* @return array | |||
*/ | |||
abstract protected function getLocalMethods(); | |||
/** | |||
* @return array E.g.: `'ClassName' => 'oc version',` | |||
*/ | |||
public function getClasses() { | |||
return array_merge($this->getLocalClasses(), $this->check->getClasses()); | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` | |||
*/ | |||
public function getConstants() { | |||
return array_merge($this->getLocalConstants(), $this->check->getConstants()); | |||
} | |||
/** | |||
* @return array E.g.: `'functionName' => 'oc version',` | |||
*/ | |||
public function getFunctions() { | |||
return array_merge($this->getLocalFunctions(), $this->check->getFunctions()); | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::methodName' => 'oc version',` | |||
*/ | |||
public function getMethods() { | |||
return array_merge($this->getLocalMethods(), $this->check->getMethods()); | |||
} | |||
/** | |||
* @return bool | |||
*/ | |||
public function checkStrongComparisons() { | |||
return $this->check->checkStrongComparisons(); | |||
} | |||
} |
@@ -1,140 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* @author Thomas Müller <thomas.mueller@tmit.eu> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
use OC\Hooks\BasicEmitter; | |||
use PhpParser\NodeTraverser; | |||
use PhpParser\Parser; | |||
use PhpParser\ParserFactory; | |||
use RecursiveCallbackFilterIterator; | |||
use RecursiveDirectoryIterator; | |||
use RecursiveIteratorIterator; | |||
use RegexIterator; | |||
use SplFileInfo; | |||
class CodeChecker extends BasicEmitter { | |||
public const CLASS_EXTENDS_NOT_ALLOWED = 1000; | |||
public const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001; | |||
public const STATIC_CALL_NOT_ALLOWED = 1002; | |||
public const CLASS_CONST_FETCH_NOT_ALLOWED = 1003; | |||
public const CLASS_NEW_NOT_ALLOWED = 1004; | |||
public const OP_OPERATOR_USAGE_DISCOURAGED = 1005; | |||
public const CLASS_USE_NOT_ALLOWED = 1006; | |||
public const CLASS_METHOD_CALL_NOT_ALLOWED = 1007; | |||
/** @var Parser */ | |||
private $parser; | |||
/** @var ICheck */ | |||
protected $checkList; | |||
/** @var bool */ | |||
protected $checkMigrationSchema; | |||
public function __construct(ICheck $checkList, $checkMigrationSchema) { | |||
$this->checkList = $checkList; | |||
$this->checkMigrationSchema = $checkMigrationSchema; | |||
$this->parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7); | |||
} | |||
/** | |||
* @param string $appId | |||
* @return array | |||
* @throws \RuntimeException if app with $appId is unknown | |||
*/ | |||
public function analyse(string $appId): array { | |||
$appPath = \OC_App::getAppPath($appId); | |||
if ($appPath === false) { | |||
throw new \RuntimeException("No app with given id <$appId> known."); | |||
} | |||
return $this->analyseFolder($appId, $appPath); | |||
} | |||
/** | |||
* @param string $appId | |||
* @param string $folder | |||
* @return array | |||
*/ | |||
public function analyseFolder(string $appId, string $folder): array { | |||
$errors = []; | |||
$excludedDirectories = ['vendor', '3rdparty', '.git', 'l10n', 'tests', 'test', 'build']; | |||
if ($appId === 'password_policy') { | |||
$excludedDirectories[] = 'lists'; | |||
} | |||
$excludes = array_map(function ($item) use ($folder) { | |||
return $folder . '/' . $item; | |||
}, $excludedDirectories); | |||
$iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS); | |||
$iterator = new RecursiveCallbackFilterIterator($iterator, function ($item) use ($excludes) { | |||
/** @var SplFileInfo $item */ | |||
foreach ($excludes as $exclude) { | |||
if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) { | |||
return false; | |||
} | |||
} | |||
return true; | |||
}); | |||
$iterator = new RecursiveIteratorIterator($iterator); | |||
$iterator = new RegexIterator($iterator, '/^.+\.php$/i'); | |||
foreach ($iterator as $file) { | |||
/** @var SplFileInfo $file */ | |||
$this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]); | |||
$fileErrors = $this->analyseFile($file->__toString()); | |||
$this->emit('CodeChecker', 'analyseFileFinished', [$file->getPathname(), $fileErrors]); | |||
$errors = array_merge($fileErrors, $errors); | |||
} | |||
return $errors; | |||
} | |||
/** | |||
* @param string $file | |||
* @return array | |||
*/ | |||
public function analyseFile(string $file): array { | |||
$code = file_get_contents($file); | |||
$statements = $this->parser->parse($code); | |||
$visitor = new NodeVisitor($this->checkList); | |||
$migrationVisitor = new MigrationSchemaChecker(); | |||
$traverser = new NodeTraverser; | |||
$traverser->addVisitor($visitor); | |||
if ($this->checkMigrationSchema && preg_match('#^.+\\/Migration\\/Version[^\\/]{1,255}\\.php$#i', $file)) { | |||
$traverser->addVisitor($migrationVisitor); | |||
} | |||
$traverser->traverse($statements); | |||
return array_merge($visitor->errors, $migrationVisitor->errors); | |||
} | |||
} |
@@ -1,106 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2017, Joas Schilling <coding@schilljs.com> | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class DatabaseSchemaChecker { | |||
/** | |||
* @param string $appId | |||
* @return array | |||
*/ | |||
public function analyse($appId) { | |||
$appPath = \OC_App::getAppPath($appId); | |||
if ($appPath === false) { | |||
throw new \RuntimeException("No app with given id <$appId> known."); | |||
} | |||
if (!file_exists($appPath . '/appinfo/database.xml')) { | |||
return ['errors' => [], 'warnings' => []]; | |||
} | |||
libxml_use_internal_errors(true); | |||
$loadEntities = libxml_disable_entity_loader(false); | |||
$xml = simplexml_load_file($appPath . '/appinfo/database.xml'); | |||
libxml_disable_entity_loader($loadEntities); | |||
$errors = $warnings = []; | |||
foreach ($xml->table as $table) { | |||
// Table names | |||
if (strpos((string)$table->name, '*dbprefix*') !== 0) { | |||
$errors[] = 'Database schema error: name of table ' . (string)$table->name . ' does not start with *dbprefix*'; | |||
} | |||
$tableName = substr((string)$table->name, strlen('*dbprefix*')); | |||
if (strpos($tableName, '*dbprefix*') !== false) { | |||
$warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . (string)$table->name; | |||
} | |||
if (strlen($tableName) > 27) { | |||
$errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; | |||
} | |||
$hasAutoIncrement = false; | |||
// Column names | |||
foreach ($table->declaration->field as $column) { | |||
if (strpos((string)$column->name, '*dbprefix*') !== false) { | |||
$warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . (string)$column->name . ' on table ' . (string)$table->name; | |||
} | |||
if (strlen((string)$column->name) > 30) { | |||
$errors[] = 'Database schema error: Name of column ' . (string)$column->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed'; | |||
} | |||
if ($column->autoincrement) { | |||
if ($hasAutoIncrement) { | |||
$errors[] = 'Database schema error: Table ' . (string)$table->name . ' has multiple autoincrement columns'; | |||
} | |||
if (strlen($tableName) > 21) { | |||
$errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed'; | |||
} | |||
$hasAutoIncrement = true; | |||
} | |||
} | |||
// Index names | |||
foreach ($table->declaration->index as $index) { | |||
$hasPrefix = strpos((string)$index->name, '*dbprefix*'); | |||
if ($hasPrefix !== false && $hasPrefix !== 0) { | |||
$warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . (string)$index->name . ' on table ' . (string)$table->name; | |||
} | |||
$indexName = $hasPrefix === 0 ? substr((string)$index->name, strlen('*dbprefix*')) : (string)$index->name; | |||
if (strlen($indexName) > 27) { | |||
$errors[] = 'Database schema error: Name of index ' . (string)$index->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed'; | |||
} | |||
} | |||
} | |||
return ['errors' => $errors, 'warnings' => $warnings]; | |||
} | |||
} |
@@ -1,196 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class DeprecationCheck extends AbstractCheck { | |||
/** | |||
* @return string | |||
*/ | |||
protected function getLocalDescription() { | |||
return 'deprecated'; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName' => 'oc version',` | |||
*/ | |||
protected function getLocalClasses() { | |||
return [ | |||
'OC_JSON' => '8.2.0', | |||
'OCP\API' => '9.1.0', | |||
'OCP\Contacts' => '8.1.0', | |||
'OCP\DB' => '8.1.0', | |||
'OCP\JSON' => '8.1.0', | |||
'OCP\Response' => '8.1.0', | |||
'OCP\AppFramework\IApi' => '8.0.0', | |||
'OCP\User' => '13.0.0', | |||
'OCP\BackgroundJob' => '14.0.0', | |||
'OCP\App' => '14.0.0', | |||
'OCP\Files' => '14.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` | |||
*/ | |||
protected function getLocalConstants() { | |||
return [ | |||
'OCP\API::GUEST_AUTH' => '9.1.0', | |||
'OCP\API::USER_AUTH' => '9.1.0', | |||
'OCP\API::SUBADMIN_AUTH' => '9.1.0', | |||
'OCP\API::ADMIN_AUTH' => '9.1.0', | |||
'OCP\API::RESPOND_UNAUTHORISED' => '9.1.0', | |||
'OCP\API::RESPOND_SERVER_ERROR' => '9.1.0', | |||
'OCP\API::RESPOND_NOT_FOUND' => '9.1.0', | |||
'OCP\API::RESPOND_UNKNOWN_ERROR' => '9.1.0', | |||
'OC_API::GUEST_AUTH' => '8.2.0', | |||
'OC_API::USER_AUTH' => '8.2.0', | |||
'OC_API::SUBADMIN_AUTH' => '8.2.0', | |||
'OC_API::ADMIN_AUTH' => '8.2.0', | |||
'OC_API::RESPOND_UNAUTHORISED' => '8.2.0', | |||
'OC_API::RESPOND_SERVER_ERROR' => '8.2.0', | |||
'OC_API::RESPOND_NOT_FOUND' => '8.2.0', | |||
'OC_API::RESPOND_UNKNOWN_ERROR' => '8.2.0', | |||
'OCP::PERMISSION_CREATE' => '8.0.0', | |||
'OCP::PERMISSION_READ' => '8.0.0', | |||
'OCP::PERMISSION_UPDATE' => '8.0.0', | |||
'OCP::PERMISSION_DELETE' => '8.0.0', | |||
'OCP::PERMISSION_SHARE' => '8.0.0', | |||
'OCP::PERMISSION_ALL' => '8.0.0', | |||
'OCP::FILENAME_INVALID_CHARS' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'functionName' => 'oc version',` | |||
*/ | |||
protected function getLocalFunctions() { | |||
return [ | |||
'OCP::image_path' => '8.0.0', | |||
'OCP::mimetype_icon' => '8.0.0', | |||
'OCP::preview_icon' => '8.0.0', | |||
'OCP::publicPreview_icon' => '8.0.0', | |||
'OCP::human_file_size' => '8.0.0', | |||
'OCP::relative_modified_date' => '8.0.0', | |||
'OCP::simple_file_size' => '8.0.0', | |||
'OCP::html_select_options' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::methodName' => 'oc version',` | |||
*/ | |||
protected function getLocalMethods() { | |||
return [ | |||
'OC_L10N::get' => '8.2.0', | |||
'OCP\Activity\IManager::publishActivity' => '8.2.0', | |||
'OCP\App::register' => '8.1.0', | |||
'OCP\App::addNavigationEntry' => '8.1.0', | |||
'OCP\App::getActiveNavigationEntry' => '8.2.0', | |||
'OCP\App::setActiveNavigationEntry' => '8.1.0', | |||
'OCP\App::registerPersonal' => '14.0.0', | |||
'OCP\App::registerAdmin' => '14.0.0', | |||
'OC_App::getAppInfo' => '14.0.0', | |||
'OCP\App::getAppInfo' => '14.0.0', | |||
'OC_App::getAppVersion' => '14.0.0', | |||
'OCP\App::getAppVersion' => '14.0.0', | |||
'OCP\AppFramework\Controller::params' => '7.0.0', | |||
'OCP\AppFramework\Controller::getParams' => '7.0.0', | |||
'OCP\AppFramework\Controller::method' => '7.0.0', | |||
'OCP\AppFramework\Controller::getUploadedFile' => '7.0.0', | |||
'OCP\AppFramework\Controller::env' => '7.0.0', | |||
'OCP\AppFramework\Controller::cookie' => '7.0.0', | |||
'OCP\AppFramework\Controller::render' => '7.0.0', | |||
'OCP\AppFramework\IAppContainer::getCoreApi' => '8.0.0', | |||
'OCP\AppFramework\IAppContainer::isLoggedIn' => '8.0.0', | |||
'OCP\AppFramework\IAppContainer::isAdminUser' => '8.0.0', | |||
'OCP\AppFramework\IAppContainer::log' => '8.0.0', | |||
'OCP\BackgroundJob::registerJob' => '8.1.0', | |||
'OCP\BackgroundJob::getExecutionType' => '14.0.0', | |||
'OCP\BackgroundJob::setExecutionType' => '14.0.0', | |||
'OCP\Files::tmpFile' => '8.1.0', | |||
'OCP\Files::tmpFolder' => '8.1.0', | |||
'OCP\IAppConfig::getValue' => '8.0.0', | |||
'OCP\IAppConfig::deleteKey' => '8.0.0', | |||
'OCP\IAppConfig::getKeys' => '8.0.0', | |||
'OCP\IAppConfig::setValue' => '8.0.0', | |||
'OCP\IAppConfig::deleteApp' => '8.0.0', | |||
'OCP\IDBConnection::createQueryBuilder' => '8.2.0', | |||
'OCP\IDBConnection::getExpressionBuilder' => '8.2.0', | |||
'OCP\ISearch::search' => '8.0.0', | |||
'OCP\IServerContainer::getCache' => '8.2.0', | |||
'OCP\IServerContainer::getDb' => '8.1.0', | |||
'OCP\IServerContainer::getHTTPHelper' => '8.1.0', | |||
'OCP\Response::disableCaching' => '14.0.0', | |||
'OCP\User::getUser' => '8.0.0', | |||
'OCP\User::getUsers' => '8.1.0', | |||
'OCP\User::getDisplayName' => '8.1.0', | |||
'OCP\User::getDisplayNames' => '8.1.0', | |||
'OCP\User::userExists' => '8.1.0', | |||
'OCP\User::logout' => '8.1.0', | |||
'OCP\User::checkPassword' => '8.1.0', | |||
'OCP\User::isLoggedIn' => '13.0.0', | |||
'OCP\User::checkAdminUser' => '13.0.0', | |||
'OCP\User::checkLoggedIn' => '13.0.0', | |||
'OCP\Util::encryptedFiles' => '8.1.0', | |||
'OCP\Util::formatDate' => '8.0.0', | |||
'OCP\Util::generateRandomBytes' => '8.1.0', | |||
'OCP\Util::getServerHost' => '8.1.0', | |||
'OCP\Util::getServerProtocol' => '8.1.0', | |||
'OCP\Util::getRequestUri' => '8.1.0', | |||
'OCP\Util::getScriptName' => '8.1.0', | |||
'OCP\Util::imagePath' => '8.1.0', | |||
'OCP\Util::isValidFileName' => '8.1.0', | |||
'OCP\Util::linkToRoute' => '8.1.0', | |||
'OCP\Util::linkTo' => '8.1.0', | |||
'OCP\Util::logException' => '8.2.0', | |||
'OCP\Util::mb_str_replace' => '8.2.0', | |||
'OCP\Util::mb_substr_replace' => '8.2.0', | |||
'OCP\Util::sendMail' => '8.1.0', | |||
'OCP\Util::writeLog' => '13.0.0', | |||
'OCP\Files::rmdirr' => '14.0.0', | |||
'OCP\Files::getMimeType' => '14.0.0', | |||
'OCP\Files::searchByMime' => '14.0.0', | |||
'OCP\Files::streamCopy' => '14.0.0', | |||
'OCP\Files::buildNotExistingFileName' => '14.0.0', | |||
'OCP\Files::getStorage' => '14.0.0', | |||
]; | |||
} | |||
} |
@@ -1,70 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class EmptyCheck implements ICheck { | |||
/** | |||
* @param int $errorCode | |||
* @param string $errorObject | |||
* @return string | |||
*/ | |||
public function getDescription($errorCode, $errorObject) { | |||
return ''; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName' => 'oc version',` | |||
*/ | |||
public function getClasses() { | |||
return []; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` | |||
*/ | |||
public function getConstants() { | |||
return []; | |||
} | |||
/** | |||
* @return array E.g.: `'functionName' => 'oc version',` | |||
*/ | |||
public function getFunctions() { | |||
return []; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::methodName' => 'oc version',` | |||
*/ | |||
public function getMethods() { | |||
return []; | |||
} | |||
/** | |||
* @return bool | |||
*/ | |||
public function checkStrongComparisons() { | |||
return false; | |||
} | |||
} |
@@ -1,58 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
interface ICheck { | |||
/** | |||
* @param int $errorCode | |||
* @param string $errorObject | |||
* @return string | |||
*/ | |||
public function getDescription($errorCode, $errorObject); | |||
/** | |||
* @return array E.g.: `'ClassName' => 'oc version',` | |||
*/ | |||
public function getClasses(); | |||
/** | |||
* @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` | |||
*/ | |||
public function getConstants(); | |||
/** | |||
* @return array E.g.: `'functionName' => 'oc version',` | |||
*/ | |||
public function getFunctions(); | |||
/** | |||
* @return array E.g.: `'ClassName::methodName' => 'oc version',` | |||
*/ | |||
public function getMethods(); | |||
/** | |||
* @return bool | |||
*/ | |||
public function checkStrongComparisons(); | |||
} |
@@ -1,108 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
use OC\Hooks\BasicEmitter; | |||
class InfoChecker extends BasicEmitter { | |||
/** @var string[] */ | |||
private $shippedApps; | |||
/** @var string[] */ | |||
private $alwaysEnabled; | |||
/** | |||
* @param string $appId | |||
* @return array | |||
* @throws \RuntimeException | |||
*/ | |||
public function analyse($appId): array { | |||
$appPath = \OC_App::getAppPath($appId); | |||
if ($appPath === false) { | |||
throw new \RuntimeException("No app with given id <$appId> known."); | |||
} | |||
$xml = new \DOMDocument(); | |||
$xml->load($appPath . '/appinfo/info.xml'); | |||
$schema = \OC::$SERVERROOT . '/resources/app-info.xsd'; | |||
try { | |||
if ($this->isShipped($appId)) { | |||
// Shipped apps are allowed to have the public and default_enabled tags | |||
$schema = \OC::$SERVERROOT . '/resources/app-info-shipped.xsd'; | |||
} | |||
} catch (\Exception $e) { | |||
// Assume it is not shipped | |||
} | |||
$errors = []; | |||
if (!$xml->schemaValidate($schema)) { | |||
foreach (libxml_get_errors() as $error) { | |||
$errors[] = [ | |||
'type' => 'parseError', | |||
'field' => $error->message, | |||
]; | |||
$this->emit('InfoChecker', 'parseError', [$error->message]); | |||
} | |||
} | |||
return $errors; | |||
} | |||
/** | |||
* This is a copy of \OC\App\AppManager::isShipped(), keep both in sync. | |||
* This method is copied, so the code checker works even when Nextcloud is | |||
* not installed yet. The AppManager requires a database connection, which | |||
* fails in that case. | |||
* | |||
* @param string $appId | |||
* @return bool | |||
* @throws \Exception | |||
*/ | |||
protected function isShipped(string $appId): bool { | |||
$this->loadShippedJson(); | |||
return \in_array($appId, $this->shippedApps, true); | |||
} | |||
/** | |||
* This is a copy of \OC\App\AppManager::loadShippedJson(), keep both in sync | |||
* This method is copied, so the code checker works even when Nextcloud is | |||
* not installed yet. The AppManager requires a database connection, which | |||
* fails in that case. | |||
* | |||
* @throws \Exception | |||
*/ | |||
protected function loadShippedJson() { | |||
if ($this->shippedApps === null) { | |||
$shippedJson = \OC::$SERVERROOT . '/core/shipped.json'; | |||
if (!file_exists($shippedJson)) { | |||
throw new \Exception("File not found: $shippedJson"); | |||
} | |||
$content = json_decode(file_get_contents($shippedJson), true); | |||
$this->shippedApps = $content['shippedApps']; | |||
$this->alwaysEnabled = $content['alwaysEnabled']; | |||
} | |||
} | |||
} |
@@ -1,60 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2017, Joas Schilling <coding@schilljs.com> | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class LanguageParseChecker { | |||
/** | |||
* @param string $appId | |||
* @return array | |||
*/ | |||
public function analyse($appId) { | |||
$appPath = \OC_App::getAppPath($appId); | |||
if ($appPath === false) { | |||
throw new \RuntimeException("No app with given id <$appId> known."); | |||
} | |||
if (!is_dir($appPath . '/l10n/')) { | |||
return []; | |||
} | |||
$errors = []; | |||
$directory = new \DirectoryIterator($appPath . '/l10n/'); | |||
foreach ($directory as $file) { | |||
if ($file->getExtension() !== 'json') { | |||
continue; | |||
} | |||
$content = file_get_contents($file->getPathname()); | |||
json_decode($content, true); | |||
if (json_last_error() !== JSON_ERROR_NONE) { | |||
$errors[] = 'Invalid language file found: l10n/' . $file->getFilename() . ': ' . json_last_error_msg(); | |||
} | |||
} | |||
return $errors; | |||
} | |||
} |
@@ -1,206 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> | |||
* | |||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Lukas Reschke <lukas@statuscode.ch> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
use PhpParser\Node; | |||
use PhpParser\Node\Name; | |||
use PhpParser\NodeVisitorAbstract; | |||
class MigrationSchemaChecker extends NodeVisitorAbstract { | |||
/** @var string */ | |||
protected $schemaVariableName = null; | |||
/** @var array */ | |||
protected $tableVariableNames = []; | |||
/** @var array */ | |||
public $errors = []; | |||
/** | |||
* @param Node $node | |||
* @return void | |||
* | |||
* @suppress PhanUndeclaredProperty | |||
*/ | |||
public function enterNode(Node $node) { | |||
/** | |||
* Check tables | |||
*/ | |||
if ($this->schemaVariableName !== null && | |||
$node instanceof Node\Expr\Assign && | |||
$node->var instanceof Node\Expr\Variable && | |||
$node->expr instanceof Node\Expr\MethodCall && | |||
$node->expr->var instanceof Node\Expr\Variable && | |||
$node->expr->var->name === $this->schemaVariableName) { | |||
if ($node->expr->name === 'createTable') { | |||
if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { | |||
if (!$this->checkNameLength($node->expr->args[0]->value->value)) { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => $node->expr->args[0]->value->value, | |||
'reason' => 'Table name is too long (max. 27)', | |||
]; | |||
} else { | |||
$this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; | |||
} | |||
} | |||
} elseif ($node->expr->name === 'getTable') { | |||
if (isset($node->expr->args[0]) && $node->expr->args[0]->value instanceof Node\Scalar\String_) { | |||
$this->tableVariableNames[$node->var->name] = $node->expr->args[0]->value->value; | |||
} | |||
} | |||
} elseif ($this->schemaVariableName !== null && | |||
$node instanceof Node\Expr\MethodCall && | |||
$node->var instanceof Node\Expr\Variable && | |||
$node->var->name === $this->schemaVariableName) { | |||
if ($node->name === 'renameTable') { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => 'Deprecated method', | |||
'reason' => sprintf( | |||
'`$%s->renameTable()` must not be used', | |||
$node->var->name | |||
), | |||
]; | |||
} | |||
/** | |||
* Check columns and Indexes | |||
*/ | |||
} elseif (!empty($this->tableVariableNames) && | |||
$node instanceof Node\Expr\MethodCall && | |||
$node->var instanceof Node\Expr\Variable && | |||
isset($this->tableVariableNames[$node->var->name])) { | |||
if ($node->name === 'addColumn' || $node->name === 'changeColumn') { | |||
if (isset($node->args[0]) && $node->args[0]->value instanceof Node\Scalar\String_) { | |||
if (!$this->checkNameLength($node->args[0]->value->value)) { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => $node->args[0]->value->value, | |||
'reason' => sprintf( | |||
'Column name is too long on table `%s` (max. 27)', | |||
$this->tableVariableNames[$node->var->name] | |||
), | |||
]; | |||
} | |||
// On autoincrement the max length of the table name is 21 instead of 27 | |||
if (isset($node->args[2]) && $node->args[2]->value instanceof Node\Expr\Array_) { | |||
/** @var Node\Expr\Array_ $options */ | |||
$options = $node->args[2]->value; | |||
if ($this->checkColumnForAutoincrement($options)) { | |||
if (!$this->checkNameLength($this->tableVariableNames[$node->var->name], true)) { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => $this->tableVariableNames[$node->var->name], | |||
'reason' => 'Table name is too long because of autoincrement (max. 21)', | |||
]; | |||
} | |||
} | |||
} | |||
} | |||
} elseif ($node->name === 'addIndex' || | |||
$node->name === 'addUniqueIndex' || | |||
$node->name === 'renameIndex' || | |||
$node->name === 'setPrimaryKey') { | |||
if (isset($node->args[1]) && $node->args[1]->value instanceof Node\Scalar\String_) { | |||
if (!$this->checkNameLength($node->args[1]->value->value)) { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => $node->args[1]->value->value, | |||
'reason' => sprintf( | |||
'Index name is too long on table `%s` (max. 27)', | |||
$this->tableVariableNames[$node->var->name] | |||
), | |||
]; | |||
} | |||
} | |||
} elseif ($node->name === 'addForeignKeyConstraint') { | |||
if (isset($node->args[4]) && $node->args[4]->value instanceof Node\Scalar\String_) { | |||
if (!$this->checkNameLength($node->args[4]->value->value)) { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => $node->args[4]->value->value, | |||
'reason' => sprintf( | |||
'Constraint name is too long on table `%s` (max. 27)', | |||
$this->tableVariableNames[$node->var->name] | |||
), | |||
]; | |||
} | |||
} | |||
} elseif ($node->name === 'renameColumn') { | |||
$this->errors[] = [ | |||
'line' => $node->getLine(), | |||
'disallowedToken' => 'Deprecated method', | |||
'reason' => sprintf( | |||
'`$%s->renameColumn()` must not be used', | |||
$node->var->name | |||
), | |||
]; | |||
} | |||
/** | |||
* Find the schema | |||
*/ | |||
} elseif ($node instanceof Node\Expr\Assign && | |||
$node->expr instanceof Node\Expr\FuncCall && | |||
$node->var instanceof Node\Expr\Variable && | |||
$node->expr->name instanceof Node\Expr\Variable && | |||
$node->expr->name->name === 'schemaClosure') { | |||
// E.g. $schema = $schemaClosure(); | |||
$this->schemaVariableName = $node->var->name; | |||
} | |||
} | |||
protected function checkNameLength($tableName, $hasAutoincrement = false) { | |||
if ($hasAutoincrement) { | |||
return strlen($tableName) <= 21; | |||
} | |||
return strlen($tableName) <= 27; | |||
} | |||
/** | |||
* @param Node\Expr\Array_ $optionsArray | |||
* @return bool Whether the column is an autoincrement column | |||
*/ | |||
protected function checkColumnForAutoincrement(Node\Expr\Array_ $optionsArray) { | |||
foreach ($optionsArray->items as $option) { | |||
if ($option->key instanceof Node\Scalar\String_) { | |||
if ($option->key->value === 'autoincrement' && | |||
$option->value instanceof Node\Expr\ConstFetch) { | |||
/** @var Node\Expr\ConstFetch $const */ | |||
$const = $option->value; | |||
if ($const->name instanceof Name && | |||
$const->name->parts === ['true']) { | |||
return true; | |||
} | |||
} | |||
} | |||
} | |||
return false; | |||
} | |||
} |
@@ -1,310 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | |||
* @author Daniel Kesselberg <mail@danielkesselberg.de> | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* @author Thomas Müller <thomas.mueller@tmit.eu> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
use PhpParser\Node; | |||
use PhpParser\Node\Name; | |||
use PhpParser\NodeVisitorAbstract; | |||
class NodeVisitor extends NodeVisitorAbstract { | |||
/** @var ICheck */ | |||
protected $list; | |||
/** @var string */ | |||
protected $blackListDescription; | |||
/** @var string[] */ | |||
protected $blackListedClassNames; | |||
/** @var string[] */ | |||
protected $blackListedConstants; | |||
/** @var string[] */ | |||
protected $blackListedFunctions; | |||
/** @var string[] */ | |||
protected $blackListedMethods; | |||
/** @var bool */ | |||
protected $checkEqualOperatorUsage; | |||
/** @var string[] */ | |||
protected $errorMessages; | |||
/** | |||
* @param ICheck $list | |||
*/ | |||
public function __construct(ICheck $list) { | |||
$this->list = $list; | |||
$this->blackListedClassNames = []; | |||
foreach ($list->getClasses() as $class => $blackListInfo) { | |||
if (is_numeric($class) && is_string($blackListInfo)) { | |||
$class = $blackListInfo; | |||
$blackListInfo = null; | |||
} | |||
$class = strtolower($class); | |||
$this->blackListedClassNames[$class] = $class; | |||
} | |||
$this->blackListedConstants = []; | |||
foreach ($list->getConstants() as $constantName => $blackListInfo) { | |||
$constantName = strtolower($constantName); | |||
$this->blackListedConstants[$constantName] = $constantName; | |||
} | |||
$this->blackListedFunctions = []; | |||
foreach ($list->getFunctions() as $functionName => $blackListInfo) { | |||
$functionName = strtolower($functionName); | |||
$this->blackListedFunctions[$functionName] = $functionName; | |||
} | |||
$this->blackListedMethods = []; | |||
foreach ($list->getMethods() as $functionName => $blackListInfo) { | |||
$functionName = strtolower($functionName); | |||
$this->blackListedMethods[$functionName] = $functionName; | |||
} | |||
$this->checkEqualOperatorUsage = $list->checkStrongComparisons(); | |||
$this->errorMessages = [ | |||
CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended", | |||
CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented", | |||
CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called", | |||
CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched", | |||
CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated", | |||
CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement", | |||
CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called", | |||
CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged", | |||
]; | |||
} | |||
/** @var array */ | |||
public $errors = []; | |||
public function enterNode(Node $node) { | |||
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) { | |||
$this->errors[] = [ | |||
'disallowedToken' => '==', | |||
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) | |||
]; | |||
} | |||
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) { | |||
$this->errors[] = [ | |||
'disallowedToken' => '!=', | |||
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) | |||
]; | |||
} | |||
if ($node instanceof Node\Stmt\Class_) { | |||
if (!is_null($node->extends)) { | |||
$this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node); | |||
} | |||
foreach ($node->implements as $implements) { | |||
$this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node); | |||
} | |||
} | |||
if ($node instanceof Node\Expr\StaticCall) { | |||
if (!is_null($node->class)) { | |||
if ($node->class instanceof Name) { | |||
$this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node); | |||
$this->checkBlackListFunction($node->class->toString(), $node->name, $node); | |||
$this->checkBlackListMethod($node->class->toString(), $node->name, $node); | |||
} | |||
if ($node->class instanceof Node\Expr\Variable) { | |||
/** | |||
* TODO: find a way to detect something like this: | |||
* $c = "OC_API"; | |||
* $n = $c::call(); | |||
*/ | |||
// $this->checkBlackListMethod($node->class->..., $node->name, $node); | |||
} | |||
} | |||
} | |||
if ($node instanceof Node\Expr\MethodCall) { | |||
if (!is_null($node->var)) { | |||
if ($node->var instanceof Node\Expr\Variable) { | |||
/** | |||
* TODO: find a way to detect something like this: | |||
* $c = new OC_API(); | |||
* $n = $c::call(); | |||
* $n = $c->call(); | |||
*/ | |||
// $this->checkBlackListMethod($node->var->..., $node->name, $node); | |||
} | |||
} | |||
} | |||
if ($node instanceof Node\Expr\ClassConstFetch) { | |||
if (!is_null($node->class)) { | |||
if ($node->class instanceof Name) { | |||
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node); | |||
} | |||
if ($node->class instanceof Node\Expr\Variable || $node->class instanceof Node\Expr\PropertyFetch) { | |||
/** | |||
* TODO: find a way to detect something like this: | |||
* $c = "OC_API"; | |||
* $n = $i::ADMIN_AUTH; | |||
*/ | |||
} else { | |||
$this->checkBlackListConstant($node->class->toString(), $node->name, $node); | |||
} | |||
} | |||
} | |||
if ($node instanceof Node\Expr\New_) { | |||
if (!is_null($node->class)) { | |||
if ($node->class instanceof Name) { | |||
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node); | |||
} | |||
if ($node->class instanceof Node\Expr\Variable) { | |||
/** | |||
* TODO: find a way to detect something like this: | |||
* $c = "OC_API"; | |||
* $n = new $i; | |||
*/ | |||
} | |||
} | |||
} | |||
if ($node instanceof Node\Stmt\UseUse) { | |||
$this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node); | |||
if ($node->alias) { | |||
$this->addUseNameToBlackList($node->name->toString(), $node->alias); | |||
} else { | |||
$this->addUseNameToBlackList($node->name->toString(), $node->name->getLast()); | |||
} | |||
} | |||
} | |||
/** | |||
* Check whether an alias was introduced for a namespace of a blacklisted class | |||
* | |||
* Example: | |||
* - Blacklist entry: OCP\AppFramework\IApi | |||
* - Name: OCP\AppFramework | |||
* - Alias: OAF | |||
* => new blacklist entry: OAF\IApi | |||
* | |||
* @param string $name | |||
* @param string $alias | |||
*/ | |||
private function addUseNameToBlackList($name, $alias) { | |||
$name = strtolower($name); | |||
$alias = strtolower($alias); | |||
foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) { | |||
if (strpos($blackListedClassName, $name . '\\') === 0) { | |||
$aliasedClassName = str_replace($name, $alias, $blackListedClassName); | |||
$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName; | |||
} | |||
} | |||
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) { | |||
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) { | |||
$aliasedConstantName = str_replace($name, $alias, $blackListedConstant); | |||
$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant; | |||
} | |||
} | |||
foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) { | |||
if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) { | |||
$aliasedFunctionName = str_replace($name, $alias, $blackListedFunction); | |||
$this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction; | |||
} | |||
} | |||
foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) { | |||
if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) { | |||
$aliasedMethodName = str_replace($name, $alias, $blackListedMethod); | |||
$this->blackListedMethods[$aliasedMethodName] = $blackListedMethod; | |||
} | |||
} | |||
} | |||
private function checkBlackList($name, $errorCode, Node $node) { | |||
$lowerName = strtolower($name); | |||
if (isset($this->blackListedClassNames[$lowerName])) { | |||
$this->errors[] = [ | |||
'disallowedToken' => $name, | |||
'errorCode' => $errorCode, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode) | |||
]; | |||
} | |||
} | |||
private function checkBlackListConstant($class, $constantName, Node $node) { | |||
$name = $class . '::' . $constantName; | |||
$lowerName = strtolower($name); | |||
if (isset($this->blackListedConstants[$lowerName])) { | |||
$this->errors[] = [ | |||
'disallowedToken' => $name, | |||
'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED) | |||
]; | |||
} | |||
} | |||
private function checkBlackListFunction($class, $functionName, Node $node) { | |||
$name = $class . '::' . $functionName; | |||
$lowerName = strtolower($name); | |||
if (isset($this->blackListedFunctions[$lowerName])) { | |||
$this->errors[] = [ | |||
'disallowedToken' => $name, | |||
'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED) | |||
]; | |||
} | |||
} | |||
private function checkBlackListMethod($class, $functionName, Node $node) { | |||
$name = $class . '::' . $functionName; | |||
$lowerName = strtolower($name); | |||
if (isset($this->blackListedMethods[$lowerName])) { | |||
$this->errors[] = [ | |||
'disallowedToken' => $name, | |||
'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED, | |||
'line' => $node->getLine(), | |||
'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED) | |||
]; | |||
} | |||
} | |||
private function buildReason($name, $errorCode) { | |||
if (isset($this->errorMessages[$errorCode])) { | |||
$desc = $this->list->getDescription($errorCode, $name); | |||
return sprintf($this->errorMessages[$errorCode], $desc); | |||
} | |||
return "$name usage not allowed - error: $errorCode"; | |||
} | |||
} |
@@ -1,86 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class PrivateCheck extends AbstractCheck { | |||
/** | |||
* @return string | |||
*/ | |||
protected function getLocalDescription() { | |||
return 'private'; | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getLocalClasses() { | |||
return [ | |||
// classes replaced by the public api | |||
'OC_API' => '6.0.0', | |||
'OC_App' => '6.0.0', | |||
'OC_AppConfig' => '6.0.0', | |||
'OC_Avatar' => '6.0.0', | |||
'OC_BackgroundJob' => '6.0.0', | |||
'OC_Config' => '6.0.0', | |||
'OC_DB' => '6.0.0', | |||
'OC_Files' => '6.0.0', | |||
'OC_Helper' => '6.0.0', | |||
'OC_Hook' => '6.0.0', | |||
'OC_Image' => '6.0.0', | |||
'OC_JSON' => '6.0.0', | |||
'OC_L10N' => '6.0.0', | |||
'OC_Log' => '6.0.0', | |||
'OC_Mail' => '6.0.0', | |||
'OC_Preferences' => '6.0.0', | |||
'OC_Search_Provider' => '6.0.0', | |||
'OC_Search_Result' => '6.0.0', | |||
'OC_Request' => '6.0.0', | |||
'OC_Response' => '6.0.0', | |||
'OC_Template' => '6.0.0', | |||
'OC_User' => '6.0.0', | |||
'OC_Util' => '6.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getLocalConstants() { | |||
return []; | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getLocalFunctions() { | |||
return []; | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getLocalMethods() { | |||
return []; | |||
} | |||
} |
@@ -1,79 +0,0 @@ | |||
<?php | |||
/** | |||
* @copyright Copyright (c) 2016, ownCloud, Inc. | |||
* | |||
* @author Joas Schilling <coding@schilljs.com> | |||
* | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace OC\App\CodeChecker; | |||
class StrongComparisonCheck implements ICheck { | |||
/** @var ICheck */ | |||
protected $check; | |||
/** | |||
* @param ICheck $check | |||
*/ | |||
public function __construct(ICheck $check) { | |||
$this->check = $check; | |||
} | |||
/** | |||
* @param int $errorCode | |||
* @param string $errorObject | |||
* @return string | |||
*/ | |||
public function getDescription($errorCode, $errorObject) { | |||
return $this->check->getDescription($errorCode, $errorObject); | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getClasses() { | |||
return $this->check->getClasses(); | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getConstants() { | |||
return $this->check->getConstants(); | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getFunctions() { | |||
return $this->check->getFunctions(); | |||
} | |||
/** | |||
* @return array | |||
*/ | |||
public function getMethods() { | |||
return $this->check->getMethods(); | |||
} | |||
/** | |||
* @return bool | |||
*/ | |||
public function checkStrongComparisons() { | |||
return true; | |||
} | |||
} |
@@ -1,74 +0,0 @@ | |||
<?php | |||
/** | |||
* Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> | |||
* This file is licensed under the Affero General Public License version 3 or | |||
* later. | |||
* See the COPYING-README file. | |||
*/ | |||
namespace Test\App\CodeChecker; | |||
use OC\App\CodeChecker\CodeChecker; | |||
use OC\App\CodeChecker\EmptyCheck; | |||
use OC\App\CodeChecker\PrivateCheck; | |||
use Test\TestCase; | |||
class CodeCheckerTest extends TestCase { | |||
/** | |||
* @dataProvider providesFilesToCheck | |||
* @param string $expectedErrorToken | |||
* @param int $expectedErrorCode | |||
* @param string $fileToVerify | |||
*/ | |||
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { | |||
if (PHP_MAJOR_VERSION > 7) { | |||
$this->markTestSkipped('Only run on php7'); | |||
} | |||
$checker = new CodeChecker( | |||
new PrivateCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(1, count($errors)); | |||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); | |||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); | |||
} | |||
public function providesFilesToCheck() { | |||
return [ | |||
['OC_Hook', 1000, 'test-extends.php'], | |||
['oC_Avatar', 1001, 'test-implements.php'], | |||
['OC_App', 1002, 'test-static-call.php'], | |||
['OC_API', 1003, 'test-const.php'], | |||
['OC_AppConfig', 1004, 'test-new.php'], | |||
['OC_AppConfig', 1006, 'test-use.php'], | |||
]; | |||
} | |||
/** | |||
* @dataProvider validFilesData | |||
* @param string $fileToVerify | |||
*/ | |||
public function testPassValidUsage($fileToVerify) { | |||
if (PHP_MAJOR_VERSION > 7) { | |||
$this->markTestSkipped('Only run on php7'); | |||
} | |||
$checker = new CodeChecker( | |||
new PrivateCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(0, count($errors)); | |||
} | |||
public function validFilesData() { | |||
return [ | |||
['test-identical-operator.php'], | |||
]; | |||
} | |||
} |
@@ -1,76 +0,0 @@ | |||
<?php | |||
/** | |||
* Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> | |||
* This file is licensed under the Affero General Public License version 3 or | |||
* later. | |||
* See the COPYING-README file. | |||
*/ | |||
namespace Test\App\CodeChecker; | |||
use OC\App\CodeChecker\CodeChecker; | |||
use OC\App\CodeChecker\DeprecationCheck; | |||
use OC\App\CodeChecker\EmptyCheck; | |||
use Test\TestCase; | |||
class DeprecationCheckTest extends TestCase { | |||
/** | |||
* @dataProvider providesFilesToCheck | |||
* @param string $expectedErrorToken | |||
* @param int $expectedErrorCode | |||
* @param string $fileToVerify | |||
*/ | |||
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { | |||
if (PHP_MAJOR_VERSION > 7) { | |||
$this->markTestSkipped('Only run on php7'); | |||
} | |||
$checker = new CodeChecker( | |||
new DeprecationCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(1, count($errors)); | |||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); | |||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); | |||
} | |||
public function providesFilesToCheck() { | |||
return [ | |||
['OCP\AppFramework\IApi', 1006, 'test-deprecated-use.php'], | |||
['OCP\AppFramework\IApi', 1006, 'test-deprecated-use-alias.php'], | |||
['AppFramework\IApi', 1001, 'test-deprecated-use-sub.php'], | |||
['OAF\IApi', 1001, 'test-deprecated-use-sub-alias.php'], | |||
['OC_API::ADMIN_AUTH', 1003, 'test-const.php'], | |||
]; | |||
} | |||
/** | |||
* @dataProvider validFilesData | |||
* @param string $fileToVerify | |||
*/ | |||
public function testPassValidUsage($fileToVerify) { | |||
$checker = new CodeChecker( | |||
new DeprecationCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(0, count($errors)); | |||
} | |||
public function validFilesData() { | |||
return [ | |||
['test-equal.php'], | |||
['test-not-equal.php'], | |||
['test-extends.php'], | |||
['test-implements.php'], | |||
['test-static-call.php'], | |||
['test-new.php'], | |||
['test-use.php'], | |||
['test-identical-operator.php'], | |||
]; | |||
} | |||
} |
@@ -1,76 +0,0 @@ | |||
<?php | |||
/** | |||
* @author Morris Jobke <hey@morrisjobke.de> | |||
* | |||
* @copyright Copyright (c) 2015, ownCloud, Inc. | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace Test\App\CodeChecker; | |||
use OC\App\CodeChecker\InfoChecker; | |||
use Test\TestCase; | |||
class InfoCheckerTest extends TestCase { | |||
/** @var InfoChecker */ | |||
protected $infoChecker; | |||
public static function setUpBeforeClass(): void { | |||
\OC::$APPSROOTS[] = [ | |||
'path' => \OC::$SERVERROOT . '/tests/apps', | |||
'url' => '/apps-test', | |||
'writable' => false, | |||
]; | |||
} | |||
public static function tearDownAfterClass(): void { | |||
// remove last element | |||
array_pop(\OC::$APPSROOTS); | |||
} | |||
protected function setUp(): void { | |||
parent::setUp(); | |||
$this->infoChecker = new InfoChecker(); | |||
} | |||
public function appInfoData() { | |||
return [ | |||
['testapp_infoxml', []], | |||
['testapp_version', [ | |||
['type' => 'parseError', 'field' => 'Element \'licence\': This element is not expected. Expected is one of ( description, version ).' . "\n"], | |||
]], | |||
['testapp_dependency_missing', [ | |||
['type' => 'parseError', 'field' => 'Element \'info\': Missing child element(s). Expected is one of ( repository, screenshot, dependencies ).' . "\n"], | |||
]], | |||
['testapp_name_missing', [ | |||
['type' => 'parseError', 'field' => 'Element \'summary\': This element is not expected. Expected is ( name ).' . "\n"], | |||
]], | |||
]; | |||
} | |||
/** | |||
* @dataProvider appInfoData | |||
* | |||
* @param $appId | |||
* @param $expectedErrors | |||
*/ | |||
public function testApps($appId, $expectedErrors) { | |||
$errors = $this->infoChecker->analyse($appId); | |||
libxml_clear_errors(); | |||
$this->assertEquals($expectedErrors, $errors); | |||
} | |||
} |
@@ -1,92 +0,0 @@ | |||
<?php | |||
/** | |||
* @author Joas Schilling <nickvergessen@owncloud.com> | |||
* | |||
* @copyright Copyright (c) 2015, ownCloud, Inc. | |||
* @license AGPL-3.0 | |||
* | |||
* This code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License, version 3, | |||
* as published by the Free Software Foundation. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License, version 3, | |||
* along with this program. If not, see <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace Test\App\CodeChecker\Mock; | |||
use OC\App\CodeChecker\ICheck; | |||
class TestList implements ICheck { | |||
/** @var ICheck */ | |||
protected $check; | |||
/** | |||
* @param ICheck $check | |||
*/ | |||
public function __construct(ICheck $check) { | |||
$this->check = $check; | |||
} | |||
/** | |||
* @param int $errorCode | |||
* @param string $errorObject | |||
* @return string | |||
*/ | |||
public function getDescription($errorCode, $errorObject) { | |||
return 'testing'; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName' => 'oc version',` | |||
*/ | |||
public function getClasses() { | |||
return [ | |||
// Deprecated classes | |||
'OCP\AppFramework\IApi' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` | |||
*/ | |||
public function getConstants() { | |||
return [ | |||
// Deprecated constants | |||
'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'functionName' => 'oc version',` | |||
*/ | |||
public function getFunctions() { | |||
return [ | |||
// Deprecated functions | |||
'OCP\NamespaceName\ClassName::functionName' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return array E.g.: `'ClassName::methodName' => 'oc version',` | |||
*/ | |||
public function getMethods() { | |||
return [ | |||
// Deprecated methods | |||
'OCP\NamespaceName\ClassName::methodName' => '8.0.0', | |||
]; | |||
} | |||
/** | |||
* @return bool | |||
*/ | |||
public function checkStrongComparisons() { | |||
return true; | |||
} | |||
} |
@@ -1,77 +0,0 @@ | |||
<?php | |||
/** | |||
* Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> | |||
* This file is licensed under the Affero General Public License version 3 or | |||
* later. | |||
* See the COPYING-README file. | |||
*/ | |||
namespace Test\App\CodeChecker; | |||
use OC\App\CodeChecker\CodeChecker; | |||
use OC\App\CodeChecker\EmptyCheck; | |||
use Test\App\CodeChecker\Mock\TestList; | |||
use Test\TestCase; | |||
class NodeVisitorTest extends TestCase { | |||
public function providesFilesToCheck() { | |||
return [ | |||
[[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use.php'], | |||
[[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use-alias.php'], | |||
[[['AppFramework\IApi', 1001]], 'test-deprecated-use-sub.php'], | |||
[[['OAF\IApi', 1001]], 'test-deprecated-use-sub-alias.php'], | |||
[[['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant.php'], | |||
[[['Alias::CONSTANT_NAME', 1003]], 'test-deprecated-constant-alias.php'], | |||
[[['NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub.php'], | |||
[[['SubAlias\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub-alias.php'], | |||
[[ | |||
['OCP\NamespaceName\ClassName::functionName', 1002], | |||
['OCP\NamespaceName\ClassName::methodName', 1007], | |||
], 'test-deprecated-function.php'], | |||
[[ | |||
['Alias::functionName', 1002], | |||
['Alias::methodName', 1007], | |||
], 'test-deprecated-function-alias.php'], | |||
[[ | |||
['NamespaceName\ClassName::functionName', 1002], | |||
['NamespaceName\ClassName::methodName', 1007], | |||
], 'test-deprecated-function-sub.php'], | |||
[[ | |||
['SubAlias\ClassName::functionName', 1002], | |||
['SubAlias\ClassName::methodName', 1007], | |||
], 'test-deprecated-function-sub-alias.php'], | |||
// TODO Failing to resolve variables to classes | |||
// [[['OCP\NamespaceName\ClassName::methodName', 1007]], 'test-deprecated-method.php'], | |||
// [[['Alias::methodName', 1002]], 'test-deprecated-method-alias.php'], | |||
// [[['NamespaceName\ClassName::methodName', 1002]], 'test-deprecated-method-sub.php'], | |||
// [[['SubAlias\ClassName::methodName', 1002]], 'test-deprecated-method-sub-alias.php'], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providesFilesToCheck | |||
* @param array $expectedErrors | |||
* @param string $fileToVerify | |||
*/ | |||
public function testMethodsToCheck($expectedErrors, $fileToVerify) { | |||
if (PHP_MAJOR_VERSION > 7) { | |||
$this->markTestSkipped('Only run on php7'); | |||
} | |||
$checker = new CodeChecker( | |||
new TestList(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertCount(sizeof($expectedErrors), $errors); | |||
foreach ($expectedErrors as $int => $expectedError) { | |||
$this->assertEquals($expectedError[0], $errors[$int]['disallowedToken']); | |||
$this->assertEquals($expectedError[1], $errors[$int]['errorCode']); | |||
} | |||
} | |||
} |
@@ -1,76 +0,0 @@ | |||
<?php | |||
/** | |||
* Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> | |||
* This file is licensed under the Affero General Public License version 3 or | |||
* later. | |||
* See the COPYING-README file. | |||
*/ | |||
namespace Test\App\CodeChecker; | |||
use OC\App\CodeChecker\CodeChecker; | |||
use OC\App\CodeChecker\EmptyCheck; | |||
use OC\App\CodeChecker\StrongComparisonCheck; | |||
use Test\TestCase; | |||
class StrongComparisonCheckTest extends TestCase { | |||
/** | |||
* @dataProvider providesFilesToCheck | |||
* @param string $expectedErrorToken | |||
* @param int $expectedErrorCode | |||
* @param string $fileToVerify | |||
*/ | |||
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { | |||
$checker = new CodeChecker( | |||
new StrongComparisonCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(1, count($errors)); | |||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); | |||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); | |||
} | |||
public function providesFilesToCheck() { | |||
return [ | |||
['==', 1005, 'test-equal.php'], | |||
['!=', 1005, 'test-not-equal.php'], | |||
]; | |||
} | |||
/** | |||
* @dataProvider validFilesData | |||
* @param string $fileToVerify | |||
*/ | |||
public function testPassValidUsage($fileToVerify) { | |||
if (PHP_MAJOR_VERSION > 7) { | |||
$this->markTestSkipped('Only run on php7'); | |||
} | |||
$checker = new CodeChecker( | |||
new StrongComparisonCheck(new EmptyCheck()), | |||
false | |||
); | |||
$errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
$this->assertEquals(0, count($errors)); | |||
} | |||
public function validFilesData() { | |||
return [ | |||
['test-deprecated-use.php'], | |||
['test-deprecated-use-alias.php'], | |||
['test-deprecated-use-sub.php'], | |||
['test-deprecated-use-sub-alias.php'], | |||
['test-extends.php'], | |||
['test-implements.php'], | |||
['test-static-call.php'], | |||
['test-const.php'], | |||
['test-new.php'], | |||
['test-use.php'], | |||
['test-identical-operator.php'], | |||
]; | |||
} | |||
} |