From f20d4b15c7e4a60fef9176ae6489c0978e6fbc87 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 1 Aug 2016 04:24:13 -0700 Subject: [PATCH] Add a lint error about use of PHP short array syntax ('[...]') 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 --- .../ArcanistPHPCompatibilityXHPASTLinterRule.php | 13 +++++++++++++ .../php-compatibility/php54-features.lint-test | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php index e9f4ab71..c7790eb2 100644 --- a/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php +++ b/src/lint/linter/xhpast/rules/ArcanistPHPCompatibilityXHPASTLinterRule.php @@ -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 diff --git a/src/lint/linter/xhpast/rules/__tests__/php-compatibility/php54-features.lint-test b/src/lint/linter/xhpast/rules/__tests__/php-compatibility/php54-features.lint-test index 4fb4e6c0..d7d6a768 100644 --- a/src/lint/linter/xhpast/rules/__tests__/php-compatibility/php54-features.lint-test +++ b/src/lint/linter/xhpast/rules/__tests__/php-compatibility/php54-features.lint-test @@ -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 ~~~~~~~~~~ ~~~~~~~~~~ { -- 2.43.0