]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for spacing after a cast
authorJoshua Spence <josh@joshuaspence.com>
Sun, 17 May 2015 22:06:05 +0000 (08:06 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Sun, 17 May 2015 22:07:43 +0000 (08:07 +1000)
Summary: Ref T7409. Add a linter rule to ensure that a cast is not followed by a space. This is largely based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/Formatting/NoSpaceAfterCastSniff.php | Generic_Sniffs_Formatting_NoSpaceAfterCastSniff]].

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Maniphest Tasks: T7409

Differential Revision: https://secure.phabricator.com/D12321

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

index 76ea6043d02dfb4d0e2c95135a63fed15d7c2af5..6ee850a15d0c2d281d6a8e62368e07de016bd14c 100644 (file)
@@ -65,6 +65,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_USELESS_OVERRIDING_METHOD  = 63;
   const LINT_NO_PARENT_SCOPE            = 64;
   const LINT_ALIAS_FUNCTION             = 65;
+  const LINT_CAST_SPACING               = 66;
 
   private $blacklistedFunctions = array();
   private $naminghook;
@@ -203,6 +204,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
         => pht('No Parent Scope'),
       self::LINT_ALIAS_FUNCTION
         => pht('Alias Functions'),
+      self::LINT_CAST_SPACING
+        => pht('Cast Spacing'),
     );
   }
 
@@ -255,6 +258,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_CLASS_NAME_LITERAL         => $advice,
       self::LINT_USELESS_OVERRIDING_METHOD  => $advice,
       self::LINT_ALIAS_FUNCTION             => $advice,
+      self::LINT_CAST_SPACING               => $advice,
     );
   }
 
@@ -322,7 +326,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '27';
+    return '28';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -414,6 +418,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
       'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
       'lintAliasFunctions' => self::LINT_ALIAS_FUNCTION,
+      'lintCastSpacing' => self::LINT_CAST_SPACING,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -4100,6 +4105,25 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  private function lintCastSpacing(XHPASTNode $root) {
+    $cast_expressions = $root->selectDescendantsOfType('n_CAST_EXPRESSION');
+
+    foreach ($cast_expressions as $cast_expression) {
+      $cast = $cast_expression->getChildOfType(0, 'n_CAST');
+
+      list($before, $after) = $cast->getSurroundingNonsemanticTokens();
+      $after = head($after);
+
+      if ($after) {
+        $this->raiseLintAtToken(
+          $after,
+          self::LINT_CAST_SPACING,
+          pht('A cast statement must not be followed by a space.'),
+          '');
+      }
+    }
+  }
+
 
   /**
    * Retrieve all calls to some specified function(s).
diff --git a/src/lint/linter/__tests__/xhpast/cast-spacing.lint-test b/src/lint/linter/__tests__/xhpast/cast-spacing.lint-test
new file mode 100644 (file)
index 0000000..a561155
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+echo (double)0;
+echo (int) 1;
+echo (string)  2;
+~~~~~~~~~~
+advice:3:11
+advice:4:14
+~~~~~~~~~~
+<?php
+echo (double)0;
+echo (int)1;
+echo (string)2;