@@ -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); | |||
@@ -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]; |
@@ -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', | |||
]; | |||
} |
@@ -3,3 +3,4 @@ | |||
use OCP\NamespaceName\ClassName as Alias; | |||
Alias::functionName(); | |||
Alias::methodName(); |
@@ -3,3 +3,4 @@ | |||
use OCP\NamespaceName as SubAlias; | |||
SubAlias\ClassName::functionName(); | |||
SubAlias\ClassName::methodName(); |
@@ -3,3 +3,4 @@ | |||
use OCP\NamespaceName; | |||
NamespaceName\ClassName::functionName(); | |||
NamespaceName\ClassName::methodName(); |
@@ -1,3 +1,4 @@ | |||
<?php | |||
\OCP\NamespaceName\ClassName::functionName(); | |||
\OCP\NamespaceName\ClassName::methodName(); |
@@ -0,0 +1,5 @@ | |||
<?php | |||
$class = new \OCP\NamespaceName\ClassName(); | |||
$class->methodName(); | |||
$class::methodName(); |
@@ -15,73 +15,55 @@ class CodeCheckVisitor 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\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 providesFilesToCheck | |||
* @param string $expectedErrorToken | |||
* @param int $expectedErrorCode | |||
* @param string $fileToVerify | |||
*/ | |||
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { | |||
$checker = new \Test\App\Mock\CodeChecker(); | |||
$errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); | |||
[[['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'], | |||
$this->assertEquals(1, count($errors)); | |||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); | |||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); | |||
} | |||
[[ | |||
['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'], | |||
public function providesConstantsToCheck() { | |||
return [ | |||
['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'], | |||
// 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 providesConstantsToCheck | |||
* @param string $expectedErrorToken | |||
* @param int $expectedErrorCode | |||
* @dataProvider providesFilesToCheck | |||
* @param array $expectedErrors | |||
* @param string $fileToVerify | |||
*/ | |||
public function testConstantsToCheck($expectedErrorToken, $expectedErrorCode, $fileToVerify) { | |||
public function testMethodsToCheck($expectedErrors, $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']); | |||
} | |||
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->assertCount(sizeof($expectedErrors), $errors); | |||
$this->assertEquals(1, count($errors)); | |||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); | |||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); | |||
foreach ($expectedErrors as $int => $expectedError) { | |||
$this->assertEquals($expectedError[0], $errors[$int]['disallowedToken']); | |||
$this->assertEquals($expectedError[1], $errors[$int]['errorCode']); | |||
} | |||
} | |||
} |
@@ -38,7 +38,12 @@ class CodeChecker extends \OC\App\CodeChecker { | |||
]; | |||
protected $blackListedFunctions = [ | |||
// Deprecated constants | |||
// Deprecated functions | |||
'OCP\NamespaceName\ClassName::functionName' => '8.0.0', | |||
]; | |||
protected $blackListedMethods = [ | |||
// Deprecated methods | |||
'OCP\NamespaceName\ClassName::methodName' => '8.0.0', | |||
]; | |||
} |