Check usage of != and == - refs #16054
This commit is contained in:
parent
5aa3525479
commit
aae098c24a
|
@ -42,6 +42,7 @@ class CodeChecker extends BasicEmitter {
|
|||
const STATIC_CALL_NOT_ALLOWED = 1002;
|
||||
const CLASS_CONST_FETCH_NOT_ALLOWED = 1003;
|
||||
const CLASS_NEW_FETCH_NOT_ALLOWED = 1004;
|
||||
const OP_OPERATOR_USAGE_DISCOURAGED = 1005;
|
||||
|
||||
/** @var Parser */
|
||||
private $parser;
|
||||
|
|
|
@ -44,6 +44,22 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
public $errors = [];
|
||||
|
||||
public function enterNode(Node $node) {
|
||||
if ($node instanceof Node\Expr\BinaryOp\Equal) {
|
||||
$this->errors[]= [
|
||||
'disallowedToken' => '==',
|
||||
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
|
||||
'line' => $node->getLine(),
|
||||
'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
|
||||
];
|
||||
}
|
||||
if ($node instanceof Node\Expr\BinaryOp\NotEqual) {
|
||||
$this->errors[]= [
|
||||
'disallowedToken' => '!=',
|
||||
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
|
||||
'line' => $node->getLine(),
|
||||
'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
|
||||
];
|
||||
}
|
||||
if ($node instanceof Node\Stmt\Class_) {
|
||||
if (!is_null($node->extends)) {
|
||||
$this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node);
|
||||
|
@ -114,6 +130,7 @@ class CodeCheckVisitor extends NodeVisitorAbstract {
|
|||
CodeChecker::STATIC_CALL_NOT_ALLOWED => "static method call on private class",
|
||||
CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "used to fetch a const from",
|
||||
CodeChecker::CLASS_NEW_FETCH_NOT_ALLOWED => "is instanciated",
|
||||
CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged"
|
||||
];
|
||||
|
||||
if (isset($errorMessages[$errorCode])) {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class BadClass - uses equal instead of identical operator
|
||||
*/
|
||||
class BadClass {
|
||||
public function foo() {
|
||||
if (true == false) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class BadClass - uses equal instead of identical operator
|
||||
*/
|
||||
class BadClass {
|
||||
public function foo() {
|
||||
if (true != false) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,12 +9,14 @@
|
|||
namespace Test\App;
|
||||
|
||||
use OC;
|
||||
use Test\TestCase;
|
||||
|
||||
class CodeChecker extends \Test\TestCase {
|
||||
class CodeChecker extends TestCase {
|
||||
|
||||
/**
|
||||
* @dataProvider providesFilesToCheck
|
||||
* @param $expectedErrors
|
||||
* @param $expectedErrorToken
|
||||
* @param $expectedErrorCode
|
||||
* @param $fileToVerify
|
||||
*/
|
||||
public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) {
|
||||
|
@ -33,6 +35,8 @@ class CodeChecker extends \Test\TestCase {
|
|||
['OC_App', 1002, 'test-static-call.php'],
|
||||
['OC_API', 1003, 'test-const.php'],
|
||||
['OC_AppConfig', 1004, 'test-new.php'],
|
||||
['==', 1005, 'test-equal.php'],
|
||||
['!=', 1005, 'test-not-equal.php'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue