]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for `abstract` methods containing a body
authorJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:19:35 +0000 (08:19 +1100)
committerJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:19:35 +0000 (08:19 +1100)
Summary:
`abstract` methods cannot contain a body.

```
PHP Fatal error:  Abstract function X::Y() cannot contain body in /home/josh/workspace/github.com/phacility/arcanist/test.php on line 4
```

Test Plan: Added unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
src/lint/linter/xhpast/rules/ArcanistAbstractMethodBodyXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistClassMustBeDeclaredAbstractXHPASTLinterRule.php
src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/abstract-method-body/body.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/abstract-method-body/no-body.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/abstract-method-body/non-abstract.lint-test [new file with mode: 0644]

index ae832c381407d041b6c77339080e1a740bbd826b..c85c88249c0e7e45d9b0fa6d107233a4aca83ce2 100644 (file)
@@ -9,6 +9,8 @@
 phutil_register_library_map(array(
   '__library_version__' => 2,
   'class' => array(
+    'ArcanistAbstractMethodBodyXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistAbstractMethodBodyXHPASTLinterRule.php',
+    'ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase.php',
     'ArcanistAbstractPrivateMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistAbstractPrivateMethodXHPASTLinterRule.php',
     'ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase.php',
     'ArcanistAliasFunctionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistAliasFunctionXHPASTLinterRule.php',
@@ -395,6 +397,8 @@ phutil_register_library_map(array(
   ),
   'function' => array(),
   'xmap' => array(
+    'ArcanistAbstractMethodBodyXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistAbstractPrivateMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistAliasFunctionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
index 8c40e5c37cf1de63686a854a20876e4a676f763e..f780ec6cfdd91d9d29a7f3ca40522325d97984ed 100644 (file)
@@ -225,6 +225,34 @@ abstract class ArcanistXHPASTLinterRule extends Phobject {
     return AASTNodeList::newFromTreeAndNodes($root->getTree(), $nodes);
   }
 
+  /**
+   * Get class/method modifiers.
+   *
+   * @param  XHPASTNode         A node of type `n_CLASS_DECLARATION` or
+   *                            `n_METHOD_DECLARATION`.
+   * @return map<string, bool>  Class/method modifiers.
+   */
+  final protected function getModifiers(XHPASTNode $node) {
+    $modifier_list = $node->getChildByIndex(0);
+
+    switch ($modifier_list->getTypeName()) {
+      case 'n_CLASS_ATTRIBUTES':
+      case 'n_METHOD_MODIFIER_LIST':
+        break;
+
+      default:
+        return array();
+    }
+
+    $modifiers = array();
+
+    foreach ($modifier_list->selectDescendantsOfType('n_STRING') as $modifier) {
+      $modifiers[strtolower($modifier->getConcreteString())] = true;
+    }
+
+    return $modifiers;
+  }
+
   /**
    * Get PHP superglobals.
    *
diff --git a/src/lint/linter/xhpast/rules/ArcanistAbstractMethodBodyXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistAbstractMethodBodyXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..277e15e
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+final class ArcanistAbstractMethodBodyXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 108;
+
+  public function getLintName() {
+    return pht('`%s` Method Cannot Contain Body', 'abstract');
+  }
+
+  public function process(XHPASTNode $root) {
+    $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
+
+    foreach ($methods as $method) {
+      $modifiers = $this->getModifiers($method);
+      $body = $method->getChildByIndex(5);
+
+      if (idx($modifiers, 'abstract') && $body->getTypeName() != 'n_EMPTY') {
+        $this->raiseLintAtNode(
+          $body,
+          pht(
+            '`%s` methods cannot contain a body. This construct will '.
+            'cause a fatal error.',
+            'abstract'));
+      }
+    }
+  }
+
+}
index 1d3b4f6e8c3eaee45bfc4699227a0a2cb3a0c3fe..859dc3196ab06709b57bad1118f29a97e9b28a31 100644 (file)
@@ -43,32 +43,4 @@ final class ArcanistClassMustBeDeclaredAbstractXHPASTLinterRule
     }
   }
 
-  /**
-   * Get class/method modifiers.
-   *
-   * @param  XHPASTNode         A node of type `n_CLASS_DECLARATION` or
-   *                            `n_METHOD_DECLARATION`.
-   * @return map<string, bool>  Class/method modifiers.
-   */
-  private function getModifiers(XHPASTNode $node) {
-    $modifier_list = $node->getChildByIndex(0);
-
-    switch ($modifier_list->getTypeName()) {
-      case 'n_CLASS_ATTRIBUTES':
-      case 'n_METHOD_MODIFIER_LIST':
-        break;
-
-      default:
-        return array();
-    }
-
-    $modifiers = array();
-
-    foreach ($modifier_list->selectDescendantsOfType('n_STRING') as $modifier) {
-      $modifiers[strtolower($modifier->getConcreteString())] = true;
-    }
-
-    return $modifiers;
-  }
-
 }
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..133e6b9
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+final class ArcanistAbstractMethodBodyXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(dirname(__FILE__).'/abstract-method-body/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/body.lint-test b/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/body.lint-test
new file mode 100644 (file)
index 0000000..0ffc86e
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+abstract class SomeClass {
+  abstract public function someMethod() {}
+}
+~~~~~~~~~~
+error:3:41
diff --git a/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/no-body.lint-test b/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/no-body.lint-test
new file mode 100644 (file)
index 0000000..c2307f9
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+abstract class SomeClass {
+  abstract public function someMethod();
+}
+~~~~~~~~~~
diff --git a/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/non-abstract.lint-test b/src/lint/linter/xhpast/rules/__tests__/abstract-method-body/non-abstract.lint-test
new file mode 100644 (file)
index 0000000..b6601bf
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+abstract class SomeClass {
+  public function someMethod() {}
+}
+~~~~~~~~~~