Allow checking for functions
This commit is contained in:
parent
f228a3dc28
commit
2783a78070
|
@ -81,6 +81,8 @@ class CodeChecker extends BasicEmitter {
|
||||||
|
|
||||||
protected $blackListedConstants = [];
|
protected $blackListedConstants = [];
|
||||||
|
|
||||||
|
protected $blackListedFunctions = [];
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $checkEqualOperators = false;
|
protected $checkEqualOperators = false;
|
||||||
|
|
||||||
|
@ -146,7 +148,7 @@ class CodeChecker extends BasicEmitter {
|
||||||
$code = file_get_contents($file);
|
$code = file_get_contents($file);
|
||||||
$statements = $this->parser->parse($code);
|
$statements = $this->parser->parse($code);
|
||||||
|
|
||||||
$visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->blackListedConstants, $this->checkEqualOperators);
|
$visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->blackListedConstants, $this->blackListedFunctions, $this->checkEqualOperators);
|
||||||
$traverser = new NodeTraverser;
|
$traverser = new NodeTraverser;
|
||||||
$traverser->addVisitor($visitor);
|
$traverser->addVisitor($visitor);
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
protected $blackListedClassNames;
|
protected $blackListedClassNames;
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
protected $blackListedConstants;
|
protected $blackListedConstants;
|
||||||
|
/** @var string[] */
|
||||||
|
protected $blackListedFunctions;
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $checkEqualOperatorUsage;
|
protected $checkEqualOperatorUsage;
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
|
@ -42,9 +44,10 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
* @param string $blackListDescription
|
* @param string $blackListDescription
|
||||||
* @param array $blackListedClassNames
|
* @param array $blackListedClassNames
|
||||||
* @param array $blackListedConstants
|
* @param array $blackListedConstants
|
||||||
|
* @param array $blackListedFunctions
|
||||||
* @param bool $checkEqualOperatorUsage
|
* @param bool $checkEqualOperatorUsage
|
||||||
*/
|
*/
|
||||||
public function __construct($blackListDescription, $blackListedClassNames, $blackListedConstants, $checkEqualOperatorUsage) {
|
public function __construct($blackListDescription, $blackListedClassNames, $blackListedConstants, $blackListedFunctions, $checkEqualOperatorUsage) {
|
||||||
$this->blackListDescription = $blackListDescription;
|
$this->blackListDescription = $blackListDescription;
|
||||||
|
|
||||||
$this->blackListedClassNames = [];
|
$this->blackListedClassNames = [];
|
||||||
|
@ -59,9 +62,15 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->blackListedConstants = [];
|
$this->blackListedConstants = [];
|
||||||
foreach ($blackListedConstants as $constant => $blackListInfo) {
|
foreach ($blackListedConstants as $constantName => $blackListInfo) {
|
||||||
$constant = strtolower($constant);
|
$constantName = strtolower($constantName);
|
||||||
$this->blackListedConstants[$constant] = $constant;
|
$this->blackListedConstants[$constantName] = $constantName;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->blackListedFunctions = [];
|
||||||
|
foreach ($blackListedFunctions as $functionName => $blackListInfo) {
|
||||||
|
$functionName = strtolower($functionName);
|
||||||
|
$this->blackListedFunctions[$functionName] = $functionName;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->checkEqualOperatorUsage = $checkEqualOperatorUsage;
|
$this->checkEqualOperatorUsage = $checkEqualOperatorUsage;
|
||||||
|
@ -118,6 +127,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
* $n = $i::call();
|
* $n = $i::call();
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->checkBlackListFunction($node->class->toString(), $node->name, $node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($node instanceof Node\Expr\ClassConstFetch) {
|
if ($node instanceof Node\Expr\ClassConstFetch) {
|
||||||
|
@ -185,12 +196,17 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
|
|
||||||
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
|
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
|
||||||
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
|
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
|
||||||
$aliasedClassName = str_replace($name, $alias, $blackListedConstant);
|
$aliasedConstantName = str_replace($name, $alias, $blackListedConstant);
|
||||||
$this->blackListedConstants[$aliasedClassName] = $blackListedConstant;
|
$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = strtolower($name);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkBlackList($name, $errorCode, Node $node) {
|
private function checkBlackList($name, $errorCode, Node $node) {
|
||||||
|
@ -206,8 +222,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkBlackListConstant($class, $constants, Node $node) {
|
private function checkBlackListConstant($class, $constantName, Node $node) {
|
||||||
$name = $class . '::' . $constants;
|
$name = $class . '::' . $constantName;
|
||||||
$lowerName = strtolower($name);
|
$lowerName = strtolower($name);
|
||||||
|
|
||||||
if (isset($this->blackListedConstants[$lowerName])) {
|
if (isset($this->blackListedConstants[$lowerName])) {
|
||||||
|
@ -220,6 +236,20 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 buildReason($name, $errorCode) {
|
private function buildReason($name, $errorCode) {
|
||||||
if (isset($this->errorMessages[$errorCode])) {
|
if (isset($this->errorMessages[$errorCode])) {
|
||||||
return $this->errorMessages[$errorCode];
|
return $this->errorMessages[$errorCode];
|
||||||
|
|
|
@ -48,4 +48,16 @@ class DeprecationCodeChecker extends CodeChecker {
|
||||||
'OCP::PERMISSION_ALL' => '8.0.0',
|
'OCP::PERMISSION_ALL' => '8.0.0',
|
||||||
'OCP::FILENAME_INVALID_CHARS' => '8.0.0',
|
'OCP::FILENAME_INVALID_CHARS' => '8.0.0',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $blackListedFunctions = [
|
||||||
|
// Deprecated functions
|
||||||
|
'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',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use OCP\NamespaceName\ClassName as Constant;
|
use OCP\NamespaceName\ClassName as Alias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||||
*/
|
*/
|
||||||
class BadClass {
|
Alias::CONSTANT_NAME;
|
||||||
public function test() {
|
|
||||||
return Constant::CONSTANT_NAME;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use OCP\NamespaceName as Constant;
|
use OCP\NamespaceName as SubAlias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||||
*/
|
*/
|
||||||
class BadClass {
|
SubAlias\ClassName::CONSTANT_NAME;
|
||||||
public function test() {
|
|
||||||
return Constant\ClassName::CONSTANT_NAME;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,8 +5,4 @@ use OCP\NamespaceName;
|
||||||
/**
|
/**
|
||||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||||
*/
|
*/
|
||||||
class BadClass {
|
NamespaceName\ClassName::CONSTANT_NAME;
|
||||||
public function test() {
|
|
||||||
return NamespaceName\ClassName::CONSTANT_NAME;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,8 +3,4 @@
|
||||||
/**
|
/**
|
||||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||||
*/
|
*/
|
||||||
class BadClass {
|
\OCP\NamespaceName\ClassName::CONSTANT_NAME;
|
||||||
public function test() {
|
|
||||||
return \OCP\NamespaceName\ClassName::CONSTANT_NAME;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use OCP\NamespaceName\ClassName as Alias;
|
||||||
|
|
||||||
|
Alias::functionName();
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use OCP\NamespaceName as SubAlias;
|
||||||
|
|
||||||
|
SubAlias\ClassName::functionName();
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use OCP\NamespaceName;
|
||||||
|
|
||||||
|
NamespaceName\ClassName::functionName();
|
|
@ -0,0 +1,3 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
\OCP\NamespaceName\ClassName::functionName();
|
|
@ -40,9 +40,9 @@ class CodeCheckVisitor extends TestCase {
|
||||||
public function providesConstantsToCheck() {
|
public function providesConstantsToCheck() {
|
||||||
return [
|
return [
|
||||||
['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant.php'],
|
['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant.php'],
|
||||||
['Constant::CONSTANT_NAME', 1003, 'test-deprecated-constant-alias.php'],
|
['Alias::CONSTANT_NAME', 1003, 'test-deprecated-constant-alias.php'],
|
||||||
['NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub.php'],
|
['NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub.php'],
|
||||||
['Constant\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub-alias.php'],
|
['SubAlias\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub-alias.php'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,4 +60,28 @@ class CodeCheckVisitor extends TestCase {
|
||||||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']);
|
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']);
|
||||||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']);
|
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function providesFunctionsToCheck() {
|
||||||
|
return [
|
||||||
|
['OCP\NamespaceName\ClassName::functionName', 1002, 'test-deprecated-function.php'],
|
||||||
|
['Alias::functionName', 1002, 'test-deprecated-function-alias.php'],
|
||||||
|
['NamespaceName\ClassName::functionName', 1002, 'test-deprecated-function-sub.php'],
|
||||||
|
['SubAlias\ClassName::functionName', 1002, 'test-deprecated-function-sub-alias.php'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providesFunctionsToCheck
|
||||||
|
* @param string $expectedErrorToken
|
||||||
|
* @param int $expectedErrorCode
|
||||||
|
* @param string $fileToVerify
|
||||||
|
*/
|
||||||
|
public function testFunctionsToCheck($expectedErrorToken, $expectedErrorCode, $fileToVerify) {
|
||||||
|
$checker = new \Test\App\Mock\CodeChecker();
|
||||||
|
$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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,4 +36,9 @@ class CodeChecker extends \OC\App\CodeChecker {
|
||||||
// Deprecated constants
|
// Deprecated constants
|
||||||
'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0',
|
'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $blackListedFunctions = [
|
||||||
|
// Deprecated constants
|
||||||
|
'OCP\NamespaceName\ClassName::functionName' => '8.0.0',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue