1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-10 00:42:40 +01:00

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
This commit is contained in:
Joshua Spence 2014-05-19 06:33:22 -07:00 committed by epriestley
parent 0468be3e7f
commit 566c7e9c5c
2 changed files with 35 additions and 0 deletions

View file

@ -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',

View file

@ -0,0 +1,19 @@
<?php
if (true) {
echo 'foo';
} elseif (false) {
echo 'bar';
} else if (null) {
echo 'baz';
}
~~~~~~~~~~
advice:4:3
~~~~~~~~~~
<?php
if (true) {
echo 'foo';
} else if (false) {
echo 'bar';
} else if (null) {
echo 'baz';
}