]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve PHP compatibility linter
authorJoshua Spence <josh@joshuaspence.com>
Mon, 10 Aug 2015 20:48:56 +0000 (06:48 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 10 Aug 2015 20:48:56 +0000 (06:48 +1000)
Summary: Ref T8674. Adds to `ArcanistPHPCompatibilityXHPASTLinterRule` such that an error is raised whenever `self` or `$this` is used in an anonymous closure prior to PHP 5.4.

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Maniphest Tasks: T8674

Differential Revision: https://secure.phabricator.com/D13841

src/lint/linter/__tests__/xhpast/php54-features.lint-test
src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php

index bdeff1077cf5d1341a3f06dace6cf31b939f158b..91c112c5ac816707794194b42f38a018e0c8ae48 100644 (file)
@@ -6,9 +6,26 @@ f()[0];
 // The check above should be this, see T4334.
 // $o->m()[0];
 
+final class SomeClass extends Phobject {
+  public function someMethod() {
+    return function () {
+      $this->someOtherMethod();
+    };
+  }
+
+  public static function someStaticMethod() {
+    return function () {
+      self::someOtherMethod();
+    };
+  }
+}
+
 ~~~~~~~~~~
 error:3:5
 error:4:9
+error:9:13
+error:12:7
+error:18:7
 ~~~~~~~~~~
 ~~~~~~~~~~
 {
index 3f118b432528382cdb4cc910dc18f3cfca9b2c78..7697356e3311ee2588068af41376a6f7e9862722 100644 (file)
@@ -402,6 +402,52 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
           break;
       }
     }
+
+    $closures = $this->getAnonymousClosures($root);
+    foreach ($closures as $closure) {
+      $static_accesses = $closure
+        ->selectDescendantsOfType('n_CLASS_STATIC_ACCESS');
+
+      foreach ($static_accesses as $static_access) {
+        $class = $static_access->getChildByIndex(0);
+
+        if ($class->getTypeName() != 'n_CLASS_NAME') {
+          continue;
+        }
+
+        if (strtolower($class->getConcreteString()) != 'self') {
+          continue;
+        }
+
+        $this->raiseLintAtNode(
+          $class,
+          pht(
+            'The use of `%s` in an anonymous closure is not '.
+            'available before PHP 5.4.',
+            'self'));
+      }
+
+      $property_accesses = $closure
+        ->selectDescendantsOfType('n_OBJECT_PROPERTY_ACCESS');
+      foreach ($property_accesses as $property_access) {
+        $variable = $property_access->getChildByIndex(0);
+
+        if ($variable->getTypeName() != 'n_VARIABLE') {
+          continue;
+        }
+
+        if ($variable->getConcreteString() != '$this') {
+          continue;
+        }
+
+        $this->raiseLintAtNode(
+          $variable,
+          pht(
+            'The use of `%s` in an anonymous closure is not '.
+            'available before PHP 5.4.',
+            '$this'));
+      }
+    }
   }
 
   private function lintPHP54Incompatibilities(XHPASTNode $root) {