From: Joshua Spence Date: Mon, 19 May 2014 13:33:22 +0000 (-0700) Subject: Add a rule to the XHPAST linter for detecting `elseif` usage. X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=566c7e9c5cf43360db481e60b49a876426027afc;p=arcanist.git Add a rule to the XHPAST linter for detecting `elseif` usage. Summary: This rule is adapted from [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ElseIfDeclarationSniff.php | PHP_CodeSniffer]] and is used to enforce the consistent use of `else if`. Test Plan: Wrote and executed unit tests. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: epriestley, Korvin, neal Differential Revision: https://secure.phabricator.com/D9193 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index a1f082dd..0be11ed0 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -44,6 +44,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { const LINT_REUSED_ITERATOR_REFERENCE = 39; const LINT_KEYWORD_CASING = 40; const LINT_DOUBLE_QUOTE = 41; + const LINT_ELSEIF_USAGE = 42; private $naminghook; private $switchhook; @@ -101,6 +102,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_REUSED_ITERATOR_REFERENCE => 'Reuse of Iterator References', self::LINT_KEYWORD_CASING => 'Keyword Conventions', self::LINT_DOUBLE_QUOTE => 'Unnecessary Double Quotes', + self::LINT_ELSEIF_USAGE => 'ElseIf Usage', ); } @@ -135,6 +137,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_REUSED_ITERATOR_REFERENCE => $warning, self::LINT_KEYWORD_CASING => $warning, self::LINT_DOUBLE_QUOTE => $advice, + self::LINT_ELSEIF_USAGE => $advice, // This is disabled by default because it implies a very strict policy // which isn't necessary in the general case. @@ -249,6 +252,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { 'lintClosingDeclarationParen' => self::LINT_CLOSING_DECL_PAREN, 'lintKeywordCasing' => self::LINT_KEYWORD_CASING, 'lintStrings' => self::LINT_DOUBLE_QUOTE, + 'lintElseIfStatements' => self::LINT_ELSEIF_USAGE, ); foreach ($method_codes as $method => $codes) { @@ -2438,6 +2442,18 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } + protected function lintElseIfStatements(XHPASTNode $root) { + $tokens = $root->selectTokensOfType('T_ELSEIF'); + + foreach ($tokens as $token) { + $this->raiseLintAtToken( + $token, + self::LINT_ELSEIF_USAGE, + pht('Usage of `else if` is preferred over `elseif`.'), + 'else if'); + } + } + public function getSuperGlobalNames() { return array( '$GLOBALS', diff --git a/src/lint/linter/__tests__/xhpast/elseif.lint-test b/src/lint/linter/__tests__/xhpast/elseif.lint-test new file mode 100644 index 00000000..bebe3d6c --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/elseif.lint-test @@ -0,0 +1,19 @@ +