Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15391812
D12856.id31015.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D12856.id31015.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Sun, Mar 16, 11:47 AM (1 w, 9 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7229872
Default Alt Text
D12856.id31015.diff (2 KB)
Attached To
Mode
D12856: Add a linter rule for the instanceof operator
Attached
Detach File
Event Timeline
Log In to Comment