summaryrefslogtreecommitdiffstats
path: root/lib/private/app
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2015-06-15 18:36:04 +0200
committerJoas Schilling <nickvergessen@owncloud.com>2015-07-17 12:34:56 +0200
commiteb1c4379415c60dbb873b549683978af79d1027d (patch)
tree41ebe1bb3a9c1a9b8e4849e60079848bf43ccfea /lib/private/app
parent2783a780708e4e5c04b6935840416d219bc4d852 (diff)
downloadnextcloud-server-eb1c4379415c60dbb873b549683978af79d1027d.tar.gz
nextcloud-server-eb1c4379415c60dbb873b549683978af79d1027d.zip
Check for methods as good as possible
Diffstat (limited to 'lib/private/app')
-rw-r--r--lib/private/app/codechecker.php5
-rw-r--r--lib/private/app/codecheckvisitor.php55
-rw-r--r--lib/private/app/deprecationcodechecker.php64
3 files changed, 119 insertions, 5 deletions
diff --git a/lib/private/app/codechecker.php b/lib/private/app/codechecker.php
index b3d8a3b1e81..93f82def81e 100644
--- a/lib/private/app/codechecker.php
+++ b/lib/private/app/codechecker.php
@@ -44,6 +44,7 @@ class CodeChecker extends BasicEmitter {
const CLASS_NEW_FETCH_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;
@@ -83,6 +84,8 @@ class CodeChecker extends BasicEmitter {
protected $blackListedFunctions = [];
+ protected $blackListedMethods = [];
+
/** @var bool */
protected $checkEqualOperators = false;
@@ -148,7 +151,7 @@ class CodeChecker extends BasicEmitter {
$code = file_get_contents($file);
$statements = $this->parser->parse($code);
- $visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->blackListedConstants, $this->blackListedFunctions, $this->checkEqualOperators);
+ $visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->blackListedConstants, $this->blackListedFunctions, $this->blackListedMethods, $this->checkEqualOperators);
$traverser = new NodeTraverser;
$traverser->addVisitor($visitor);
diff --git a/lib/private/app/codecheckvisitor.php b/lib/private/app/codecheckvisitor.php
index 4741a997c7d..dd12d2faa4d 100644
--- a/lib/private/app/codecheckvisitor.php
+++ b/lib/private/app/codecheckvisitor.php
@@ -35,6 +35,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
protected $blackListedConstants;
/** @var string[] */
protected $blackListedFunctions;
+ /** @var string[] */
+ protected $blackListedMethods;
/** @var bool */
protected $checkEqualOperatorUsage;
/** @var string[] */
@@ -45,9 +47,10 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
* @param array $blackListedClassNames
* @param array $blackListedConstants
* @param array $blackListedFunctions
+ * @param array $blackListedMethods
* @param bool $checkEqualOperatorUsage
*/
- public function __construct($blackListDescription, $blackListedClassNames, $blackListedConstants, $blackListedFunctions, $checkEqualOperatorUsage) {
+ public function __construct($blackListDescription, $blackListedClassNames, $blackListedConstants, $blackListedFunctions, $blackListedMethods, $checkEqualOperatorUsage) {
$this->blackListDescription = $blackListDescription;
$this->blackListedClassNames = [];
@@ -73,6 +76,12 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
$this->blackListedFunctions[$functionName] = $functionName;
}
+ $this->blackListedMethods = [];
+ foreach ($blackListedMethods as $functionName => $blackListInfo) {
+ $functionName = strtolower($functionName);
+ $this->blackListedMethods[$functionName] = $functionName;
+ }
+
$this->checkEqualOperatorUsage = $checkEqualOperatorUsage;
$this->errorMessages = [
@@ -82,6 +91,7 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of {$this->blackListDescription} class must not not be fetched",
CodeChecker::CLASS_NEW_FETCH_NOT_ALLOWED => "{$this->blackListDescription} class must not be instanciated",
CodeChecker::CLASS_USE_NOT_ALLOWED => "{$this->blackListDescription} class must not be imported with a use statement",
+ CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of {$this->blackListDescription} class must not be called",
CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged",
];
@@ -119,16 +129,32 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
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 = $i::call();
+ * $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);
}
-
- $this->checkBlackListFunction($node->class->toString(), $node->name, $node);
}
}
if ($node instanceof Node\Expr\ClassConstFetch) {
@@ -207,6 +233,13 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
$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) {
@@ -250,6 +283,20 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
}
}
+ 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])) {
return $this->errorMessages[$errorCode];
diff --git a/lib/private/app/deprecationcodechecker.php b/lib/private/app/deprecationcodechecker.php
index 48146c74fa8..5bec83f971a 100644
--- a/lib/private/app/deprecationcodechecker.php
+++ b/lib/private/app/deprecationcodechecker.php
@@ -60,4 +60,68 @@ class DeprecationCodeChecker extends CodeChecker {
'OCP::simple_file_size' => '8.0.0',
'OCP::html_select_options' => '8.0.0',
];
+
+ protected $blackListedMethods = [
+ // Deprecated methods
+ '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',
+ ];
}