aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/command/app/checkcode.php33
-rw-r--r--lib/private/app/codechecker/abstractcheck.php139
-rw-r--r--lib/private/app/codechecker/codechecker.php (renamed from lib/private/app/codechecker.php)42
-rw-r--r--lib/private/app/codechecker/deprecationcheck.php147
-rw-r--r--lib/private/app/codechecker/emptycheck.php67
-rw-r--r--lib/private/app/codechecker/icheck.php55
-rw-r--r--lib/private/app/codechecker/nodevisitor.php306
-rw-r--r--lib/private/app/codechecker/privatecheck.php86
-rw-r--r--lib/private/app/codechecker/strongcomparisoncheck.php80
-rw-r--r--lib/private/app/codecheckvisitor.php133
-rw-r--r--tests/data/app/code-checker/test-deprecated-constant-alias.php8
-rw-r--r--tests/data/app/code-checker/test-deprecated-constant-sub-alias.php8
-rw-r--r--tests/data/app/code-checker/test-deprecated-constant-sub.php8
-rw-r--r--tests/data/app/code-checker/test-deprecated-constant.php6
-rw-r--r--tests/data/app/code-checker/test-deprecated-function-alias.php6
-rw-r--r--tests/data/app/code-checker/test-deprecated-function-sub-alias.php6
-rw-r--r--tests/data/app/code-checker/test-deprecated-function-sub.php6
-rw-r--r--tests/data/app/code-checker/test-deprecated-function.php4
-rw-r--r--tests/data/app/code-checker/test-deprecated-method.php5
-rw-r--r--tests/data/app/code-checker/test-deprecated-use-alias.php9
-rw-r--r--tests/data/app/code-checker/test-deprecated-use-sub-alias.php9
-rw-r--r--tests/data/app/code-checker/test-deprecated-use-sub.php9
-rw-r--r--tests/data/app/code-checker/test-deprecated-use.php9
-rw-r--r--tests/data/app/code-checker/test-use.php12
-rw-r--r--tests/lib/app/codechecker/codecheckertest.php (renamed from tests/lib/app/codechecker.php)31
-rw-r--r--tests/lib/app/codechecker/deprecationchecktest.php70
-rw-r--r--tests/lib/app/codechecker/mock/testlist.php92
-rw-r--r--tests/lib/app/codechecker/nodevisitortest.php73
-rw-r--r--tests/lib/app/codechecker/strongcomparisonchecktest.php70
29 files changed, 1348 insertions, 181 deletions
diff --git a/core/command/app/checkcode.php b/core/command/app/checkcode.php
index ecec51e5768..a4e7322460f 100644
--- a/core/command/app/checkcode.php
+++ b/core/command/app/checkcode.php
@@ -3,6 +3,7 @@
* @author Joas Schilling <nickvergessen@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
+ * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
@@ -23,12 +24,21 @@
namespace OC\Core\Command\App;
+use OC\App\CodeChecker\CodeChecker;
+use OC\App\CodeChecker\EmptyCheck;
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 {
+ protected $checkers = [
+ 'private' => '\OC\App\CodeChecker\PrivateCheck',
+ 'deprecation' => '\OC\App\CodeChecker\DeprecationCheck',
+ 'strong-comparison' => '\OC\App\CodeChecker\StrongComparisonCheck',
+ ];
+
protected function configure() {
$this
->setName('app:check-code')
@@ -37,12 +47,30 @@ class CheckCode extends Command {
'app-id',
InputArgument::REQUIRED,
'check the specified app'
+ )
+ ->addOption(
+ 'checker',
+ 'c',
+ InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
+ 'enable the specified checker(s)',
+ [ 'private', 'deprecation', 'strong-comparison' ]
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$appId = $input->getArgument('app-id');
- $codeChecker = new \OC\App\CodeChecker();
+
+ $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);
+
$codeChecker->listen('CodeChecker', 'analyseFileBegin', function($params) use ($output) {
if(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$output->writeln("<info>Analysing {$params}</info>");
@@ -72,9 +100,10 @@ class CheckCode extends Command {
$errors = $codeChecker->analyse($appId);
if (empty($errors)) {
$output->writeln('<info>App is compliant - awesome job!</info>');
+ return 0;
} else {
$output->writeln('<error>App is not compliant</error>');
- return 1;
+ return 101;
}
}
}
diff --git a/lib/private/app/codechecker/abstractcheck.php b/lib/private/app/codechecker/abstractcheck.php
new file mode 100644
index 00000000000..c1c6524e42f
--- /dev/null
+++ b/lib/private/app/codechecker/abstractcheck.php
@@ -0,0 +1,139 @@
+<?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 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();
+ }
+}
diff --git a/lib/private/app/codechecker.php b/lib/private/app/codechecker/codechecker.php
index 3f6cd19d6b4..ef7dc7f3e4d 100644
--- a/lib/private/app/codechecker.php
+++ b/lib/private/app/codechecker/codechecker.php
@@ -21,14 +21,13 @@
*
*/
-namespace OC\App;
+namespace OC\App\CodeChecker;
use OC\Hooks\BasicEmitter;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\NodeTraverser;
-use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
@@ -42,43 +41,20 @@ class CodeChecker extends BasicEmitter {
const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001;
const STATIC_CALL_NOT_ALLOWED = 1002;
const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
- const CLASS_NEW_FETCH_NOT_ALLOWED = 1004;
+ const CLASS_NEW_NOT_ALLOWED = 1004;
const OP_OPERATOR_USAGE_DISCOURAGED = 1005;
+ const CLASS_USE_NOT_ALLOWED = 1006;
+ const CLASS_METHOD_CALL_NOT_ALLOWED = 1007;
/** @var Parser */
private $parser;
- /** @var string[] */
- private $blackListedClassNames;
+ /** @var ICheck */
+ protected $checkList;
- public function __construct() {
+ public function __construct(ICheck $checkList) {
+ $this->checkList = $checkList;
$this->parser = new Parser(new Lexer);
- $this->blackListedClassNames = [
- // classes replaced by the public api
- 'OC_API',
- 'OC_App',
- 'OC_AppConfig',
- 'OC_Avatar',
- 'OC_BackgroundJob',
- 'OC_Config',
- 'OC_DB',
- 'OC_Files',
- 'OC_Helper',
- 'OC_Hook',
- 'OC_Image',
- 'OC_JSON',
- 'OC_L10N',
- 'OC_Log',
- 'OC_Mail',
- 'OC_Preferences',
- 'OC_Search_Provider',
- 'OC_Search_Result',
- 'OC_Request',
- 'OC_Response',
- 'OC_Template',
- 'OC_User',
- 'OC_Util',
- ];
}
/**
@@ -138,7 +114,7 @@ class CodeChecker extends BasicEmitter {
$code = file_get_contents($file);
$statements = $this->parser->parse($code);
- $visitor = new CodeCheckVisitor($this->blackListedClassNames);
+ $visitor = new NodeVisitor($this->checkList);
$traverser = new NodeTraverser;
$traverser->addVisitor($visitor);
diff --git a/lib/private/app/codechecker/deprecationcheck.php b/lib/private/app/codechecker/deprecationcheck.php
new file mode 100644
index 00000000000..3b6dc968bb5
--- /dev/null
+++ b/lib/private/app/codechecker/deprecationcheck.php
@@ -0,0 +1,147 @@
+<?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 OC\App\CodeChecker;
+
+class DeprecationCheck extends AbstractCheck implements ICheck {
+ /**
+ * @return string
+ */
+ protected function getLocalDescription() {
+ return 'deprecated';
+ }
+
+ /**
+ * @return array E.g.: `'ClassName' => 'oc version',`
+ */
+ protected function getLocalClasses() {
+ return [
+ 'OCP\Config' => '8.0.0',
+ 'OCP\Contacts' => '8.1.0',
+ 'OCP\DB' => '8.1.0',
+ 'OCP\IHelper' => '8.1.0',
+ 'OCP\JSON' => '8.1.0',
+ 'OCP\Response' => '8.1.0',
+ 'OCP\AppFramework\IApi' => '8.0.0',
+ ];
+ }
+
+ /**
+ * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',`
+ */
+ protected function getLocalConstants() {
+ return [
+ '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 [
+ 'OCP\App::register' => '8.1.0',
+ 'OCP\App::addNavigationEntry' => '8.1.0',
+ 'OCP\App::setActiveNavigationEntry' => '8.1.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::addQueuedTask' => '6.0.0',
+ 'OCP\BackgroundJob::addRegularTask' => '6.0.0',
+ 'OCP\BackgroundJob::allQueuedTasks' => '6.0.0',
+ 'OCP\BackgroundJob::allRegularTasks' => '6.0.0',
+ 'OCP\BackgroundJob::deleteQueuedTask' => '6.0.0',
+ 'OCP\BackgroundJob::findQueuedTask' => '6.0.0',
+ 'OCP\BackgroundJob::queuedTaskWhereAppIs' => '6.0.0',
+ 'OCP\BackgroundJob::registerJob' => '8.1.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\ISearch::search' => '8.0.0',
+
+ 'OCP\IServerContainer::getDb' => '8.1.0',
+ 'OCP\IServerContainer::getHTTPHelper' => '8.1.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\Util::sendMail' => '8.1.0',
+ 'OCP\Util::formatDate' => '8.0.0',
+ 'OCP\Util::encryptedFiles' => '8.1.0',
+ 'OCP\Util::linkToRoute' => '8.1.0',
+ 'OCP\Util::linkTo' => '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::generateRandomBytes' => '8.1.0',
+ 'OCP\Util::mb_str_replace' => '8.2.0',
+ 'OCP\Util::mb_substr_replace' => '8.2.0',
+ ];
+ }
+}
diff --git a/lib/private/app/codechecker/emptycheck.php b/lib/private/app/codechecker/emptycheck.php
new file mode 100644
index 00000000000..0e5df55d090
--- /dev/null
+++ b/lib/private/app/codechecker/emptycheck.php
@@ -0,0 +1,67 @@
+<?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 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;
+ }
+}
diff --git a/lib/private/app/codechecker/icheck.php b/lib/private/app/codechecker/icheck.php
new file mode 100644
index 00000000000..a00e0d8fa13
--- /dev/null
+++ b/lib/private/app/codechecker/icheck.php
@@ -0,0 +1,55 @@
+<?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 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();
+}
diff --git a/lib/private/app/codechecker/nodevisitor.php b/lib/private/app/codechecker/nodevisitor.php
new file mode 100644
index 00000000000..a22f852f36a
--- /dev/null
+++ b/lib/private/app/codechecker/nodevisitor.php
@@ -0,0 +1,306 @@
+<?php
+/**
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @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 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) {
+ /**
+ * TODO: find a way to detect something like this:
+ * $c = "OC_API";
+ * $n = $i::ADMIN_AUTH;
+ */
+ }
+
+ $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";
+ }
+}
diff --git a/lib/private/app/codechecker/privatecheck.php b/lib/private/app/codechecker/privatecheck.php
new file mode 100644
index 00000000000..d6f4eb06981
--- /dev/null
+++ b/lib/private/app/codechecker/privatecheck.php
@@ -0,0 +1,86 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @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 OC\App\CodeChecker;
+
+class PrivateCheck extends AbstractCheck implements ICheck {
+ /**
+ * @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 [];
+ }
+}
diff --git a/lib/private/app/codechecker/strongcomparisoncheck.php b/lib/private/app/codechecker/strongcomparisoncheck.php
new file mode 100644
index 00000000000..7de0fe3e5c3
--- /dev/null
+++ b/lib/private/app/codechecker/strongcomparisoncheck.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @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 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;
+ }
+}
diff --git a/lib/private/app/codecheckvisitor.php b/lib/private/app/codecheckvisitor.php
deleted file mode 100644
index e983bd8630b..00000000000
--- a/lib/private/app/codecheckvisitor.php
+++ /dev/null
@@ -1,133 +0,0 @@
-<?php
-/**
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @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 OC\App;
-
-use PhpParser\Node;
-use PhpParser\Node\Name;
-use PhpParser\NodeVisitorAbstract;
-
-class CodeCheckVisitor extends NodeVisitorAbstract {
-
- public function __construct($blackListedClassNames) {
- $this->blackListedClassNames = array_map('strtolower', $blackListedClassNames);
- }
-
- public $errors = [];
-
- public function enterNode(Node $node) {
- if ($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 ($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);
- }
- if ($node->class instanceof Node\Expr\Variable) {
- /**
- * TODO: find a way to detect something like this:
- * $c = "OC_API";
- * $n = $i::call();
- */
- }
- }
- }
- 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) {
- /**
- * TODO: find a way to detect something like this:
- * $c = "OC_API";
- * $n = $i::ADMIN_AUTH;
- */
- }
- }
- }
- if ($node instanceof Node\Expr\New_) {
- if (!is_null($node->class)) {
- if ($node->class instanceof Name) {
- $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_FETCH_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;
- */
- }
- }
- }
- }
-
- private function checkBlackList($name, $errorCode, Node $node) {
- if (in_array(strtolower($name), $this->blackListedClassNames)) {
- $this->errors[]= [
- 'disallowedToken' => $name,
- 'errorCode' => $errorCode,
- 'line' => $node->getLine(),
- 'reason' => $this->buildReason($name, $errorCode)
- ];
- }
- }
-
- private function buildReason($name, $errorCode) {
- static $errorMessages= [
- CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "used as base class",
- CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "used as interface",
- CodeChecker::STATIC_CALL_NOT_ALLOWED => "static method call on private class",
- CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "used to fetch a const from",
- CodeChecker::CLASS_NEW_FETCH_NOT_ALLOWED => "is instanciated",
- CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged"
- ];
-
- if (isset($errorMessages[$errorCode])) {
- return $errorMessages[$errorCode];
- }
-
- return "$name usage not allowed - error: $errorCode";
- }
-}
diff --git a/tests/data/app/code-checker/test-deprecated-constant-alias.php b/tests/data/app/code-checker/test-deprecated-constant-alias.php
new file mode 100644
index 00000000000..b5a5bfdb762
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-constant-alias.php
@@ -0,0 +1,8 @@
+<?php
+
+use OCP\NamespaceName\ClassName as Alias;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+Alias::CONSTANT_NAME;
diff --git a/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php b/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php
new file mode 100644
index 00000000000..9b1757aa683
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php
@@ -0,0 +1,8 @@
+<?php
+
+use OCP\NamespaceName as SubAlias;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+SubAlias\ClassName::CONSTANT_NAME;
diff --git a/tests/data/app/code-checker/test-deprecated-constant-sub.php b/tests/data/app/code-checker/test-deprecated-constant-sub.php
new file mode 100644
index 00000000000..86e0ff52efe
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-constant-sub.php
@@ -0,0 +1,8 @@
+<?php
+
+use OCP\NamespaceName;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+NamespaceName\ClassName::CONSTANT_NAME;
diff --git a/tests/data/app/code-checker/test-deprecated-constant.php b/tests/data/app/code-checker/test-deprecated-constant.php
new file mode 100644
index 00000000000..170b1d9e5ad
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-constant.php
@@ -0,0 +1,6 @@
+<?php
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+\OCP\NamespaceName\ClassName::CONSTANT_NAME;
diff --git a/tests/data/app/code-checker/test-deprecated-function-alias.php b/tests/data/app/code-checker/test-deprecated-function-alias.php
new file mode 100644
index 00000000000..28ed5051191
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-function-alias.php
@@ -0,0 +1,6 @@
+<?php
+
+use OCP\NamespaceName\ClassName as Alias;
+
+Alias::functionName();
+Alias::methodName();
diff --git a/tests/data/app/code-checker/test-deprecated-function-sub-alias.php b/tests/data/app/code-checker/test-deprecated-function-sub-alias.php
new file mode 100644
index 00000000000..73dd5814531
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-function-sub-alias.php
@@ -0,0 +1,6 @@
+<?php
+
+use OCP\NamespaceName as SubAlias;
+
+SubAlias\ClassName::functionName();
+SubAlias\ClassName::methodName();
diff --git a/tests/data/app/code-checker/test-deprecated-function-sub.php b/tests/data/app/code-checker/test-deprecated-function-sub.php
new file mode 100644
index 00000000000..c08d3bad8c0
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-function-sub.php
@@ -0,0 +1,6 @@
+<?php
+
+use OCP\NamespaceName;
+
+NamespaceName\ClassName::functionName();
+NamespaceName\ClassName::methodName();
diff --git a/tests/data/app/code-checker/test-deprecated-function.php b/tests/data/app/code-checker/test-deprecated-function.php
new file mode 100644
index 00000000000..12a144a7118
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-function.php
@@ -0,0 +1,4 @@
+<?php
+
+\OCP\NamespaceName\ClassName::functionName();
+\OCP\NamespaceName\ClassName::methodName();
diff --git a/tests/data/app/code-checker/test-deprecated-method.php b/tests/data/app/code-checker/test-deprecated-method.php
new file mode 100644
index 00000000000..ee2fdb642d4
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-method.php
@@ -0,0 +1,5 @@
+<?php
+
+$class = new \OCP\NamespaceName\ClassName();
+$class->methodName();
+$class::methodName();
diff --git a/tests/data/app/code-checker/test-deprecated-use-alias.php b/tests/data/app/code-checker/test-deprecated-use-alias.php
new file mode 100644
index 00000000000..a92187fa880
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-use-alias.php
@@ -0,0 +1,9 @@
+<?php
+
+use OCP\AppFramework\IApi as OAFIA;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+class BadClass implements OAFIA {
+}
diff --git a/tests/data/app/code-checker/test-deprecated-use-sub-alias.php b/tests/data/app/code-checker/test-deprecated-use-sub-alias.php
new file mode 100644
index 00000000000..9da4b75d216
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-use-sub-alias.php
@@ -0,0 +1,9 @@
+<?php
+
+use OCP\AppFramework as OAF;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+class BadClass implements OAF\IApi {
+}
diff --git a/tests/data/app/code-checker/test-deprecated-use-sub.php b/tests/data/app/code-checker/test-deprecated-use-sub.php
new file mode 100644
index 00000000000..a53e9a7229e
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-use-sub.php
@@ -0,0 +1,9 @@
+<?php
+
+use OCP\AppFramework;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+class BadClass implements AppFramework\IApi {
+}
diff --git a/tests/data/app/code-checker/test-deprecated-use.php b/tests/data/app/code-checker/test-deprecated-use.php
new file mode 100644
index 00000000000..ebf2c90bc5a
--- /dev/null
+++ b/tests/data/app/code-checker/test-deprecated-use.php
@@ -0,0 +1,9 @@
+<?php
+
+use OCP\AppFramework\IApi;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+class BadClass implements IApi {
+}
diff --git a/tests/data/app/code-checker/test-use.php b/tests/data/app/code-checker/test-use.php
new file mode 100644
index 00000000000..9c4824f9767
--- /dev/null
+++ b/tests/data/app/code-checker/test-use.php
@@ -0,0 +1,12 @@
+<?php
+
+use OC_AppConfig as UseConfig;
+
+/**
+ * Class BadClass - creating an instance of a blacklisted class is not allowed
+ */
+class BadClass {
+ public function foo() {
+ $bar = new UseConfig();
+ }
+}
diff --git a/tests/lib/app/codechecker.php b/tests/lib/app/codechecker/codecheckertest.php
index f45ee02d185..cdbb7c17da5 100644
--- a/tests/lib/app/codechecker.php
+++ b/tests/lib/app/codechecker/codecheckertest.php
@@ -6,22 +6,26 @@
* See the COPYING-README file.
*/
-namespace Test\App;
+namespace Test\App\CodeChecker;
-use OC;
+use OC\App\CodeChecker\CodeChecker;
+use OC\App\CodeChecker\EmptyCheck;
+use OC\App\CodeChecker\PrivateCheck;
use Test\TestCase;
-class CodeChecker extends TestCase {
+class CodeCheckerTest extends TestCase {
/**
* @dataProvider providesFilesToCheck
- * @param $expectedErrorToken
- * @param $expectedErrorCode
- * @param $fileToVerify
+ * @param string $expectedErrorToken
+ * @param int $expectedErrorCode
+ * @param string $fileToVerify
*/
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) {
- $checker = new OC\App\CodeChecker();
- $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify");
+ $checker = new CodeChecker(
+ new PrivateCheck(new EmptyCheck())
+ );
+ $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify");
$this->assertEquals(1, count($errors));
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']);
@@ -35,18 +39,19 @@ class CodeChecker extends TestCase {
['OC_App', 1002, 'test-static-call.php'],
['OC_API', 1003, 'test-const.php'],
['OC_AppConfig', 1004, 'test-new.php'],
- ['==', 1005, 'test-equal.php'],
- ['!=', 1005, 'test-not-equal.php'],
+ ['OC_AppConfig', 1006, 'test-use.php'],
];
}
/**
* @dataProvider validFilesData
- * @param $fileToVerify
+ * @param string $fileToVerify
*/
public function testPassValidUsage($fileToVerify) {
- $checker = new OC\App\CodeChecker();
- $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify");
+ $checker = new CodeChecker(
+ new PrivateCheck(new EmptyCheck())
+ );
+ $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify");
$this->assertEquals(0, count($errors));
}
diff --git a/tests/lib/app/codechecker/deprecationchecktest.php b/tests/lib/app/codechecker/deprecationchecktest.php
new file mode 100644
index 00000000000..2cf64a9d186
--- /dev/null
+++ b/tests/lib/app/codechecker/deprecationchecktest.php
@@ -0,0 +1,70 @@
+<?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) {
+ $checker = new CodeChecker(
+ new DeprecationCheck(new EmptyCheck())
+ );
+ $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'],
+ ];
+ }
+
+ /**
+ * @dataProvider validFilesData
+ * @param string $fileToVerify
+ */
+ public function testPassValidUsage($fileToVerify) {
+ $checker = new CodeChecker(
+ new DeprecationCheck(new EmptyCheck())
+ );
+ $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-const.php'],
+ ['test-new.php'],
+ ['test-use.php'],
+ ['test-identical-operator.php'],
+ ];
+ }
+}
diff --git a/tests/lib/app/codechecker/mock/testlist.php b/tests/lib/app/codechecker/mock/testlist.php
new file mode 100644
index 00000000000..1fe83293acf
--- /dev/null
+++ b/tests/lib/app/codechecker/mock/testlist.php
@@ -0,0 +1,92 @@
+<?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;
+ }
+}
diff --git a/tests/lib/app/codechecker/nodevisitortest.php b/tests/lib/app/codechecker/nodevisitortest.php
new file mode 100644
index 00000000000..0b5aea28324
--- /dev/null
+++ b/tests/lib/app/codechecker/nodevisitortest.php
@@ -0,0 +1,73 @@
+<?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\AppCodeChecker;
+
+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) {
+ $checker = new CodeChecker(
+ new TestList(new EmptyCheck())
+ );
+ $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']);
+ }
+ }
+}
diff --git a/tests/lib/app/codechecker/strongcomparisonchecktest.php b/tests/lib/app/codechecker/strongcomparisonchecktest.php
new file mode 100644
index 00000000000..c73eae286ab
--- /dev/null
+++ b/tests/lib/app/codechecker/strongcomparisonchecktest.php
@@ -0,0 +1,70 @@
+<?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())
+ );
+ $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) {
+ $checker = new CodeChecker(
+ new StrongComparisonCheck(new EmptyCheck())
+ );
+ $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'],
+ ];
+ }
+}