]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a lint error about use of PHP short array syntax ('[...]')
authorepriestley <git@epriestley.com>
Mon, 1 Aug 2016 11:24:13 +0000 (04:24 -0700)
committerepriestley <git@epriestley.com>
Mon, 1 Aug 2016 17:45:09 +0000 (10:45 -0700)
Summary: Ref T11409. Add lint to detect using `[...]` to define arrays. This doesn't work in PHP 5.2/5.3, which some of our users run.

Test Plan:
  - Ran `arc unit`.
  - Ran `arc lint src/applications/harbormaster/conduit/HarbormasterQueryBuildsConduitAPIMethod.php` to detect the issue in T11409.

Reviewers: yelirekim, chad

Reviewed By: chad

Maniphest Tasks: T11409

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

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

index e9f4ab7141b39343f5be562f231a109d53b896c0..c7790eb28bd9c2a95bec5e9682dc4ecadd46c739 100644 (file)
@@ -369,6 +369,19 @@ final class ArcanistPHPCompatibilityXHPASTLinterRule
       }
     }
 
+    $literals = $root->selectDescendantsOftype('n_ARRAY_LITERAL');
+    foreach ($literals as $literal) {
+      $open_token = head($literal->getTokens())->getValue();
+      if ($open_token == '[') {
+        $this->raiseLintAtNode(
+          $literal,
+          pht(
+            'The short array syntax ("[...]") was not introduced until '.
+            'PHP 5.4, but this codebase targets an earlier version of PHP. '.
+            'You can rewrite this expression using `array(...)` instead.'));
+      }
+    }
+
     $closures = $this->getAnonymousClosures($root);
     foreach ($closures as $closure) {
       $static_accesses = $closure
index 4fb4e6c07d26a1f9769e6e1f9602c94244df4ab3..d7d6a76802e436455eb80fe720bee19bb2282346 100644 (file)
@@ -22,12 +22,15 @@ final class SomeClass extends Phobject {
 
 0b1;
 
+[];
+
 ~~~~~~~~~~
 error:3:5
 error:4:9
 error:12:7
 error:18:7
 error:23:1
+error:25:1
 ~~~~~~~~~~
 ~~~~~~~~~~
 {