]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Detect use of f()[0] in lint
authorepriestley <git@epriestley.com>
Wed, 15 Aug 2012 11:36:50 +0000 (04:36 -0700)
committerepriestley <git@epriestley.com>
Wed, 15 Aug 2012 11:36:50 +0000 (04:36 -0700)
Summary: XHPAST doesn't currently parse most PHP 5.4 stuff, but it does parse this. Warn about it.

Test Plan:
Unit tests, and:

   Error  (XHP35) Use Of PHP 5.4 Features
    The f()[...] syntax was not introduced until PHP 5.4, but this codebase
    targets an earlier version of PHP. You can rewrite this expression using
    idx().

             365   public function lintPHP54Features($root) {
             366
             367     if (false) {
    >>>      368       id()[0];
                            ^
             369     }
             370

Reviewers: alanh, vrana

Reviewed By: alanh

CC: aran
Differential Revision: https://secure.phabricator.com/D3291

src/lint/engine/PhutilLintEngine.php
src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/index-function.lint-test [new file with mode: 0644]

index 39c257800f9e5e7ae059154b280d966f728a2c5c..d17429782dddf60a18d847904d2ce854885dc8fe 100644 (file)
@@ -92,6 +92,8 @@ class PhutilLintEngine extends ArcanistLintEngine {
           => ArcanistLintSeverity::SEVERITY_WARNING,
         ArcanistXHPASTLinter::LINT_PHP_53_FEATURES
           => ArcanistLintSeverity::SEVERITY_ERROR,
+        ArcanistXHPASTLinter::LINT_PHP_54_FEATURES
+          => ArcanistLintSeverity::SEVERITY_ERROR,
         ArcanistXHPASTLinter::LINT_COMMENT_SPACING
           => ArcanistLintSeverity::SEVERITY_ERROR,
       ));
index 8e1e1c85f44ad233a7506f74f5de64689e62e647..372b7ddb4c1bc9b4e62c8d00e13b4b1dd816c468 100644 (file)
@@ -58,6 +58,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_REUSED_AS_ITERATOR        = 32;
   const LINT_PHT_WITH_DYNAMIC_STRING   = 33;
   const LINT_COMMENT_SPACING           = 34;
+  const LINT_PHP_54_FEATURES           = 35;
 
 
   public function getLintNameMap() {
@@ -92,6 +93,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_RAGGED_CLASSTREE_EDGE     => 'Class Not abstract Or final',
       self::LINT_IMPLICIT_FALLTHROUGH      => 'Implicit Fallthrough',
       self::LINT_PHP_53_FEATURES           => 'Use Of PHP 5.3 Features',
+      self::LINT_PHP_54_FEATURES           => 'Use Of PHP 5.4 Features',
       self::LINT_REUSED_AS_ITERATOR        => 'Variable Reused As Iterator',
       self::LINT_PHT_WITH_DYNAMIC_STRING   => 'Use of pht() on Dynamic String',
       self::LINT_COMMENT_SPACING           => 'Comment Spaces',
@@ -138,6 +140,8 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       // a specific minimum version.
       self::LINT_PHP_53_FEATURES
         => ArcanistLintSeverity::SEVERITY_DISABLED,
+      self::LINT_PHP_54_FEATURES
+        => ArcanistLintSeverity::SEVERITY_DISABLED,
     );
   }
 
@@ -210,6 +214,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintRaggedClasstreeEdges($root);
     $this->lintImplicitFallthrough($root);
     $this->lintPHP53Features($root);
+    $this->lintPHP54Features($root);
     $this->lintPHT($root);
   }
 
@@ -357,6 +362,23 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
 
   }
 
+  public function lintPHP54Features($root) {
+    $indexes = $root->selectDescendantsOfType('n_INDEX_ACCESS');
+    foreach ($indexes as $index) {
+      $left = $index->getChildByIndex(0);
+      switch ($left->getTypeName()) {
+        case 'n_FUNCTION_CALL':
+          $this->raiseLintAtNode(
+            $index->getChildByIndex(1),
+            self::LINT_PHP_54_FEATURES,
+            "The f()[...] syntax was not introduced until PHP 5.4, but this ".
+            "codebase targets an earlier version of PHP. You can rewrite ".
+            "this expression using idx().");
+          break;
+      }
+    }
+  }
+
   private function lintImplicitFallthrough($root) {
     $switches = $root->selectDescendantsOfType('n_SWITCH');
     foreach ($switches as $switch) {
diff --git a/src/lint/linter/__tests__/xhpast/index-function.lint-test b/src/lint/linter/__tests__/xhpast/index-function.lint-test
new file mode 100644 (file)
index 0000000..c076d1d
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+f()[0];
+~~~~~~~~~~
+disabled:2:5