Page MenuHomePhabricator

D12856.id30926.diff
No OneTemporary

D12856.id30926.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
@@ -64,6 +64,7 @@
const LINT_CLASS_NAME_LITERAL = 62;
const LINT_USELESS_OVERRIDING_METHOD = 63;
const LINT_NO_PARENT_SCOPE = 64;
+ const LINT_INSTANCEOF_OPERATOR = 65;
private $blacklistedFunctions = array();
private $naminghook;
@@ -200,6 +201,8 @@
=> pht('Useless Overriding Method'),
self::LINT_NO_PARENT_SCOPE
=> pht('No Parent Scope'),
+ self::LINT_INSTANCEOF_OPERATOR
+ => pht('%s Operator', 'instanceof'),
);
}
@@ -318,7 +321,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '26';
+ return '27';
}
protected function resolveFuture($path, Future $future) {
@@ -409,6 +412,7 @@
'lintClassNameLiteral' => self::LINT_CLASS_NAME_LITERAL,
'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
+ 'lintInstanceOfOperator' => self::LINT_INSTANCEOF_OPERATOR,
);
foreach ($method_codes as $method => $codes) {
@@ -3887,6 +3891,29 @@
}
}
+ 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
Sat, May 18, 10:10 AM (4 w, 9 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6300517
Default Alt Text
D12856.id30926.diff (2 KB)

Event Timeline