Add support for deprecated constants
This commit is contained in:
parent
483c886291
commit
f228a3dc28
9 changed files with 202 additions and 8 deletions
|
@ -79,6 +79,8 @@ class CodeChecker extends BasicEmitter {
|
|||
'OC_Util',
|
||||
];
|
||||
|
||||
protected $blackListedConstants = [];
|
||||
|
||||
/** @var bool */
|
||||
protected $checkEqualOperators = false;
|
||||
|
||||
|
@ -144,7 +146,7 @@ class CodeChecker extends BasicEmitter {
|
|||
$code = file_get_contents($file);
|
||||
$statements = $this->parser->parse($code);
|
||||
|
||||
$visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->checkEqualOperators);
|
||||
$visitor = new CodeCheckVisitor($this->blackListDescription, $this->blackListedClassNames, $this->blackListedConstants, $this->checkEqualOperators);
|
||||
$traverser = new NodeTraverser;
|
||||
$traverser->addVisitor($visitor);
|
||||
|
||||
|
|
|
@ -31,6 +31,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
protected $blackListDescription;
|
||||
/** @var string[] */
|
||||
protected $blackListedClassNames;
|
||||
/** @var string[] */
|
||||
protected $blackListedConstants;
|
||||
/** @var bool */
|
||||
protected $checkEqualOperatorUsage;
|
||||
/** @var string[] */
|
||||
|
@ -39,9 +41,10 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
/**
|
||||
* @param string $blackListDescription
|
||||
* @param array $blackListedClassNames
|
||||
* @param array $blackListedConstants
|
||||
* @param bool $checkEqualOperatorUsage
|
||||
*/
|
||||
public function __construct($blackListDescription, $blackListedClassNames, $checkEqualOperatorUsage) {
|
||||
public function __construct($blackListDescription, $blackListedClassNames, $blackListedConstants, $checkEqualOperatorUsage) {
|
||||
$this->blackListDescription = $blackListDescription;
|
||||
|
||||
$this->blackListedClassNames = [];
|
||||
|
@ -54,6 +57,13 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
$class = strtolower($class);
|
||||
$this->blackListedClassNames[$class] = $class;
|
||||
}
|
||||
|
||||
$this->blackListedConstants = [];
|
||||
foreach ($blackListedConstants as $constant => $blackListInfo) {
|
||||
$constant = strtolower($constant);
|
||||
$this->blackListedConstants[$constant] = $constant;
|
||||
}
|
||||
|
||||
$this->checkEqualOperatorUsage = $checkEqualOperatorUsage;
|
||||
|
||||
$this->errorMessages = [
|
||||
|
@ -122,6 +132,8 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
* $n = $i::ADMIN_AUTH;
|
||||
*/
|
||||
}
|
||||
|
||||
$this->checkBlackListConstant($node->class->toString(), $node->name, $node);
|
||||
}
|
||||
}
|
||||
if ($node instanceof Node\Expr\New_) {
|
||||
|
@ -170,15 +182,40 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
|
||||
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
|
||||
$aliasedClassName = str_replace($name, $alias, $blackListedConstant);
|
||||
$this->blackListedConstants[$aliasedClassName] = $blackListedConstant;
|
||||
}
|
||||
}
|
||||
|
||||
$name = strtolower($name);
|
||||
}
|
||||
|
||||
private function checkBlackList($name, $errorCode, Node $node) {
|
||||
if (isset($this->blackListedClassNames[strtolower($name)])) {
|
||||
$lowerName = strtolower($name);
|
||||
|
||||
if (isset($this->blackListedClassNames[$lowerName])) {
|
||||
$this->errors[]= [
|
||||
'disallowedToken' => $name,
|
||||
'errorCode' => $errorCode,
|
||||
'line' => $node->getLine(),
|
||||
'reason' => $this->buildReason($this->blackListedClassNames[strtolower($name)], $errorCode)
|
||||
'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
private function checkBlackListConstant($class, $constants, Node $node) {
|
||||
$name = $class . '::' . $constants;
|
||||
$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)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,10 +21,6 @@
|
|||
|
||||
namespace OC\App;
|
||||
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
|
||||
class DeprecationCodeChecker extends CodeChecker {
|
||||
protected $checkEqualOperators = true;
|
||||
|
||||
|
@ -41,4 +37,15 @@ class DeprecationCodeChecker extends CodeChecker {
|
|||
'OCP\Response' => '8.1.0',
|
||||
'OCP\AppFramework\IApi' => '8.0.0',
|
||||
];
|
||||
|
||||
protected $blackListedConstants = [
|
||||
// Deprecated constants
|
||||
'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',
|
||||
];
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
use OCP\NamespaceName\ClassName as Constant;
|
||||
|
||||
/**
|
||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||
*/
|
||||
class BadClass {
|
||||
public function test() {
|
||||
return Constant::CONSTANT_NAME;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
use OCP\NamespaceName as Constant;
|
||||
|
||||
/**
|
||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||
*/
|
||||
class BadClass {
|
||||
public function test() {
|
||||
return Constant\ClassName::CONSTANT_NAME;
|
||||
}
|
||||
}
|
12
tests/data/app/code-checker/test-deprecated-constant-sub.php
Normal file
12
tests/data/app/code-checker/test-deprecated-constant-sub.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
use OCP\NamespaceName;
|
||||
|
||||
/**
|
||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||
*/
|
||||
class BadClass {
|
||||
public function test() {
|
||||
return NamespaceName\ClassName::CONSTANT_NAME;
|
||||
}
|
||||
}
|
10
tests/data/app/code-checker/test-deprecated-constant.php
Normal file
10
tests/data/app/code-checker/test-deprecated-constant.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class BadClass - creating an instance of a blacklisted class is not allowed
|
||||
*/
|
||||
class BadClass {
|
||||
public function test() {
|
||||
return \OCP\NamespaceName\ClassName::CONSTANT_NAME;
|
||||
}
|
||||
}
|
63
tests/lib/app/codecheckvisitor.php
Normal file
63
tests/lib/app/codecheckvisitor.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?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;
|
||||
|
||||
use OC;
|
||||
use Test\TestCase;
|
||||
|
||||
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'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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");
|
||||
|
||||
$this->assertEquals(1, count($errors));
|
||||
$this->assertEquals($expectedErrorCode, $errors[0]['errorCode']);
|
||||
$this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']);
|
||||
}
|
||||
|
||||
public function providesConstantsToCheck() {
|
||||
return [
|
||||
['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant.php'],
|
||||
['Constant::CONSTANT_NAME', 1003, 'test-deprecated-constant-alias.php'],
|
||||
['NamespaceName\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub.php'],
|
||||
['Constant\ClassName::CONSTANT_NAME', 1003, 'test-deprecated-constant-sub-alias.php'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providesConstantsToCheck
|
||||
* @param string $expectedErrorToken
|
||||
* @param int $expectedErrorCode
|
||||
* @param string $fileToVerify
|
||||
*/
|
||||
public function testConstantsToCheck($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']);
|
||||
}
|
||||
}
|
39
tests/lib/app/mock/codechecker.php
Normal file
39
tests/lib/app/mock/codechecker.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?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\Mock;
|
||||
|
||||
class CodeChecker extends \OC\App\CodeChecker {
|
||||
protected $checkEqualOperators = true;
|
||||
|
||||
/** @var string */
|
||||
protected $blackListDescription = 'deprecated';
|
||||
|
||||
protected $blackListedClassNames = [
|
||||
// Deprecated classes
|
||||
'OCP\AppFramework\IApi' => '8.0.0',
|
||||
];
|
||||
|
||||
protected $blackListedConstants = [
|
||||
// Deprecated constants
|
||||
'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0',
|
||||
];
|
||||
}
|
Loading…
Reference in a new issue