Page MenuHomePhabricator

D12856.diff
No OneTemporary

D12856.diff

diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php
--- a/src/lint/linter/ArcanistXHPASTLinter.php
+++ b/src/lint/linter/ArcanistXHPASTLinter.php
@@ -68,6 +68,7 @@
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 @@
=> 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 @@
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 @@
'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 @@
}
}
+ 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).
diff --git a/src/lint/linter/__tests__/xhpast/instanceof-operator.lint-test b/src/lint/linter/__tests__/xhpast/instanceof-operator.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/instanceof-operator.lint-test
@@ -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

File Metadata

Mime Type
text/plain
Expires
May 12 2024, 5:19 AM (4 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6289791
Default Alt Text
D12856.diff (2 KB)

Event Timeline