mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-12-23 22:10:54 +01:00
Add a linter rule for the instanceof operator
Summary: The `instanceof` operator expects the first argument to be an object instance. See http://www.phpwtf.org/instanceof-smart. Test Plan: Added test case Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley Differential Revision: https://secure.phabricator.com/D12856
This commit is contained in:
parent
0c9a037719
commit
4bb2eecdaf
2 changed files with 38 additions and 1 deletions
|
@ -68,6 +68,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
const LINT_CAST_SPACING = 66;
|
||||
const LINT_TOSTRING_EXCEPTION = 67;
|
||||
const LINT_LAMBDA_FUNC_FUNCTION = 68;
|
||||
const LINT_INSTANCEOF_OPERATOR = 69;
|
||||
|
||||
private $blacklistedFunctions = array();
|
||||
private $naminghook;
|
||||
|
@ -212,6 +213,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
=> pht('Throwing Exception in %s Method', '__toString'),
|
||||
self::LINT_LAMBDA_FUNC_FUNCTION
|
||||
=> pht('%s Function', '__lambda_func'),
|
||||
self::LINT_INSTANCEOF_OPERATOR
|
||||
=> pht('%s Operator', 'instanceof'),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -332,7 +335,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
|
||||
public function getVersion() {
|
||||
// The version number should be incremented whenever a new rule is added.
|
||||
return '30';
|
||||
return '31';
|
||||
}
|
||||
|
||||
protected function resolveFuture($path, Future $future) {
|
||||
|
@ -427,6 +430,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
'lintCastSpacing' => self::LINT_CAST_SPACING,
|
||||
'lintThrowExceptionInToStringMethod' => self::LINT_TOSTRING_EXCEPTION,
|
||||
'lintLambdaFuncFunction' => self::LINT_LAMBDA_FUNC_FUNCTION,
|
||||
'lintInstanceOfOperator' => self::LINT_INSTANCEOF_OPERATOR,
|
||||
);
|
||||
|
||||
foreach ($method_codes as $method => $codes) {
|
||||
|
@ -4196,6 +4200,30 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
|
|||
}
|
||||
}
|
||||
|
||||
private function lintInstanceOfOperator(XHPASTNode $root) {
|
||||
$expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
|
||||
|
||||
foreach ($expressions as $expression) {
|
||||
$operator = $expression->getChildOfType(1, 'n_OPERATOR');
|
||||
|
||||
if (strtolower($operator->getConcreteString()) != 'instanceof') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$object = $expression->getChildByIndex(0);
|
||||
|
||||
if ($object->isStaticScalar() ||
|
||||
$object->getTypeName() == 'n_SYMBOL_NAME') {
|
||||
$this->raiseLintAtNode(
|
||||
$object,
|
||||
self::LINT_INSTANCEOF_OPERATOR,
|
||||
pht(
|
||||
'%s expects an object instance, constant given.',
|
||||
'instanceof'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve all calls to some specified function(s).
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
var_dump('foobar' instanceof stdClass);
|
||||
var_dump(123 instanceof stdClass);
|
||||
var_dump(null instanceof stdClass);
|
||||
var_dump($x instanceof stdClass);
|
||||
~~~~~~~~~~
|
||||
error:2:10
|
||||
error:3:10
|
||||
error:4:10
|
Loading…
Reference in a new issue