]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for the instanceof operator
authorJoshua Spence <josh@joshuaspence.com>
Mon, 18 May 2015 08:00:08 +0000 (18:00 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 18 May 2015 08:02:51 +0000 (18:02 +1000)
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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/instanceof-operator.lint-test [new file with mode: 0644]

index 88e4ffea4681b936e71409e79457da93475b8dca..593d588d997f6e59f304c32b3f9d94f4063d04b4 100644 (file)
@@ -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).
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 (file)
index 0000000..ebf8423
--- /dev/null
@@ -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