summaryrefslogtreecommitdiffstats
path: root/lib/private/app
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2015-06-15 15:24:45 +0200
committerJoas Schilling <nickvergessen@owncloud.com>2015-07-17 12:34:56 +0200
commitd2fc1b230235e1aa9fa5d956e9a7edbbeef72dfb (patch)
tree853e18e8fc0c2856268d864735d3bb9de2a7af50 /lib/private/app
parent4e95031ec4e74bb99dc2dc819ddb77893564e01c (diff)
downloadnextcloud-server-d2fc1b230235e1aa9fa5d956e9a7edbbeef72dfb.tar.gz
nextcloud-server-d2fc1b230235e1aa9fa5d956e9a7edbbeef72dfb.zip
Correctly handle use statements
Diffstat (limited to 'lib/private/app')
-rw-r--r--lib/private/app/codechecker.php1
-rw-r--r--lib/private/app/codecheckvisitor.php44
2 files changed, 42 insertions, 3 deletions
diff --git a/lib/private/app/codechecker.php b/lib/private/app/codechecker.php
index 38d98255cf1..0216e61e7ae 100644
--- a/lib/private/app/codechecker.php
+++ b/lib/private/app/codechecker.php
@@ -43,6 +43,7 @@ class CodeChecker extends BasicEmitter {
const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
const CLASS_NEW_FETCH_NOT_ALLOWED = 1004;
const OP_OPERATOR_USAGE_DISCOURAGED = 1005;
+ const CLASS_USE_NOT_ALLOWED = 1006;
/** @var Parser */
private $parser;
diff --git a/lib/private/app/codecheckvisitor.php b/lib/private/app/codecheckvisitor.php
index d4b5bedbb1f..0dec5b3715d 100644
--- a/lib/private/app/codecheckvisitor.php
+++ b/lib/private/app/codecheckvisitor.php
@@ -43,7 +43,12 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
*/
public function __construct($blackListDescription, $blackListedClassNames, $checkEqualOperatorUsage) {
$this->blackListDescription = $blackListDescription;
- $this->blackListedClassNames = array_map('strtolower', $blackListedClassNames);
+
+ $this->blackListedClassNames = [];
+ foreach ($blackListedClassNames as $class) {
+ $class = strtolower($class);
+ $this->blackListedClassNames[$class] = $class;
+ }
$this->checkEqualOperatorUsage = $checkEqualOperatorUsage;
$this->errorMessages = [
@@ -52,6 +57,7 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of {$this->blackListDescription} class must not be called",
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::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged",
];
@@ -127,15 +133,47 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
}
}
}
+ 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;
+ }
+ }
}
private function checkBlackList($name, $errorCode, Node $node) {
- if (in_array(strtolower($name), $this->blackListedClassNames)) {
+ if (isset($this->blackListedClassNames[strtolower($name)])) {
$this->errors[]= [
'disallowedToken' => $name,
'errorCode' => $errorCode,
'line' => $node->getLine(),
- 'reason' => $this->buildReason($name, $errorCode)
+ 'reason' => $this->buildReason($this->blackListedClassNames[strtolower($name)], $errorCode)
];
}
}