]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a `LINT_CONSTRUCTOR_PARENTHESES` rule to `ArcanistXHPASTLinter`
authorJoshua Spence <josh@joshuaspence.com>
Tue, 7 Oct 2014 12:59:03 +0000 (23:59 +1100)
committerJoshua Spence <josh@joshuaspence.com>
Tue, 7 Oct 2014 12:59:03 +0000 (23:59 +1100)
Summary: Adds an `ArcanistXHPASTLinter::LINT_CONSTRUCTOR_PARENTHESES` rule which ensures that (possibly empty) parentheses are used when calling a constructor.

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

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

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

index 2a84ed30cffaf79d3479cda1f1bfec357d755777..37dd5adfc9c92d8e3afb0226e44a9541bad7a069 100644 (file)
@@ -48,6 +48,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_LANGUAGE_CONSTRUCT_PAREN  = 46;
   const LINT_EMPTY_STATEMENT           = 47;
   const LINT_ARRAY_SEPARATOR           = 48;
+  const LINT_CONSTRUCTOR_PARENTHESES   = 49;
 
   private $naminghook;
   private $switchhook;
@@ -107,6 +108,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_LANGUAGE_CONSTRUCT_PAREN  => 'Language Construct Parentheses',
       self::LINT_EMPTY_STATEMENT           => 'Empty Block Statement',
       self::LINT_ARRAY_SEPARATOR           => 'Array Separator',
+      self::LINT_CONSTRUCTOR_PARENTHESES   => 'Constructor Parentheses',
     );
   }
 
@@ -147,6 +149,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_LANGUAGE_CONSTRUCT_PAREN  => $warning,
       self::LINT_EMPTY_STATEMENT           => $advice,
       self::LINT_ARRAY_SEPARATOR           => $advice,
+      self::LINT_CONSTRUCTOR_PARENTHESES   => $advice,
     );
   }
 
@@ -196,7 +199,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '9';
+    return '10';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -267,6 +270,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN,
       'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT,
       'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR,
+      'lintConstructorParentheses' => self::LINT_CONSTRUCTOR_PARENTHESES,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -2792,6 +2796,23 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  private function lintConstructorParentheses(XHPASTNode $root) {
+    $nodes = $root->selectDescendantsOfType('n_NEW');
+
+    foreach ($nodes as $node) {
+      $class  = $node->getChildByIndex(0);
+      $params = $node->getChildByIndex(1);
+
+      if ($params->getTypeName() == 'n_EMPTY') {
+        $this->raiseLintAtNode(
+          $class,
+          self::LINT_CONSTRUCTOR_PARENTHESES,
+          pht('Use parentheses when invoking a constructor.'),
+          $class->getConcreteString().'()');
+      }
+    }
+  }
+
   public function getSuperGlobalNames() {
     return array(
       '$GLOBALS',
diff --git a/src/lint/linter/__tests__/xhpast/constructor-parentheses.lint-test b/src/lint/linter/__tests__/xhpast/constructor-parentheses.lint-test
new file mode 100644 (file)
index 0000000..ad76cf4
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+new Foo;
+new Bar();
+new Foo\Bar;
+~~~~~~~~~~
+advice:2:5
+advice:4:5
+~~~~~~~~~~
+<?php
+new Foo();
+new Bar();
+new Foo\Bar();