]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a rule to the XHPAST linter for detecting `elseif` usage.
authorJoshua Spence <josh@joshuaspence.com>
Mon, 19 May 2014 13:33:22 +0000 (06:33 -0700)
committerepriestley <git@epriestley.com>
Mon, 19 May 2014 13:33:22 +0000 (06:33 -0700)
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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/elseif.lint-test [new file with mode: 0644]

index a1f082dd62a7312fa360f8a2aa67a619458a44fa..0be11ed020474bf773ed5df8d063f784f16699ac 100644 (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',
diff --git a/src/lint/linter/__tests__/xhpast/elseif.lint-test b/src/lint/linter/__tests__/xhpast/elseif.lint-test
new file mode 100644 (file)
index 0000000..bebe3d6
--- /dev/null
@@ -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';
+}