From f6e26a71335e8185bda165947d6c3c862ec72526 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Tue, 5 Aug 2014 22:35:34 +1000 Subject: [PATCH] Add a linter rule for parentheses being used with PHP language constructs Summary: Fixes T5648. Test Plan: Added some test cases. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: epriestley, Korvin Maniphest Tasks: T5648 Differential Revision: https://secure.phabricator.com/D10122 --- src/lint/linter/ArcanistXHPASTLinter.php | 36 +++++++++++++- .../language-construct-parentheses.lint-test | 48 +++++++++++++++++++ .../xhpast/undeclared-variables.lint-test | 2 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/lint/linter/__tests__/xhpast/language-construct-parentheses.lint-test diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 66346556..6f1439ae 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -45,6 +45,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { const LINT_SEMICOLON_SPACING = 43; const LINT_CONCATENATION_OPERATOR = 44; const LINT_PHP_COMPATIBILITY = 45; + const LINT_LANGUAGE_CONSTRUCT_PAREN = 46; private $naminghook; private $switchhook; @@ -101,6 +102,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_SEMICOLON_SPACING => 'Semicolon Spacing', self::LINT_CONCATENATION_OPERATOR => 'Concatenation Spacing', self::LINT_PHP_COMPATIBILITY => 'PHP Compatibility', + self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses', ); } @@ -138,6 +140,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_ELSEIF_USAGE => $advice, self::LINT_SEMICOLON_SPACING => $advice, self::LINT_CONCATENATION_OPERATOR => $warning, + self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning, ); } @@ -187,7 +190,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { public function getVersion() { // The version number should be incremented whenever a new rule is added. - return '7'; + return '8'; } protected function resolveFuture($path, Future $future) { @@ -255,6 +258,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { 'lintSpaceAroundConcatenationOperators' => self::LINT_CONCATENATION_OPERATOR, 'lintPHPCompatibility' => self::LINT_PHP_COMPATIBILITY, + 'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN, ); foreach ($method_codes as $method => $codes) { @@ -2521,6 +2525,36 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } + protected function lintLanguageConstructParentheses(XHPASTNode $root) { + $nodes = $root->selectDescendantsOfTypes(array( + 'n_INCLUDE_FILE', + 'n_ECHO_LIST', + )); + + foreach ($nodes as $node) { + $child = head($node->getChildren()); + + if ($child->getTypeName() === 'n_PARENTHETICAL_EXPRESSION') { + list($before, $after) = $child->getSurroundingNonsemanticTokens(); + + $replace = preg_replace( + '/^\((.*)\)$/', + '$1', + $child->getConcreteString()); + + if (!$before) { + $replace = ' '.$replace; + } + + $this->raiseLintAtNode( + $child, + self::LINT_LANGUAGE_CONSTRUCT_PAREN, + pht('Language constructs do not require parentheses.'), + $replace); + } + } + } + public function getSuperGlobalNames() { return array( '$GLOBALS', diff --git a/src/lint/linter/__tests__/xhpast/language-construct-parentheses.lint-test b/src/lint/linter/__tests__/xhpast/language-construct-parentheses.lint-test new file mode 100644 index 00000000..90e6e215 --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/language-construct-parentheses.lint-test @@ -0,0 +1,48 @@ +