]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for `abstract private` methods
authorJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:18:06 +0000 (08:18 +1100)
committerJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:18:07 +0000 (08:18 +1100)
Summary:
Methods cannot be marked as both `abstract` and `private`. This language construct will cause a fatal error:

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

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/xhpast/rules/ArcanistAbstractPrivateMethodXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/abstract-private-method/abstract-private-method.lint-test [new file with mode: 0644]

index 40d0bfb75427305eb89718e3673c9e766de3fdbf..65f00f2c8c9f7fb286402873e7b6a07b6ea77609 100644 (file)
@@ -9,6 +9,8 @@
 phutil_register_library_map(array(
   '__library_version__' => 2,
   'class' => array(
+    'ArcanistAbstractPrivateMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistAbstractPrivateMethodXHPASTLinterRule.php',
+    'ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase.php',
     'ArcanistAliasFunctionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistAliasFunctionXHPASTLinterRule.php',
     'ArcanistAliasFunctionXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistAliasFunctionXHPASTLinterRuleTestCase.php',
     'ArcanistAliasWorkflow' => 'workflow/ArcanistAliasWorkflow.php',
@@ -391,6 +393,8 @@ phutil_register_library_map(array(
   ),
   'function' => array(),
   'xmap' => array(
+    'ArcanistAbstractPrivateMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistAliasFunctionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistAliasFunctionXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistAliasWorkflow' => 'ArcanistWorkflow',
diff --git a/src/lint/linter/xhpast/rules/ArcanistAbstractPrivateMethodXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistAbstractPrivateMethodXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..b4a95ab
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+final class ArcanistAbstractPrivateMethodXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 107;
+
+  public function getLintName() {
+    return pht('`%s` Method Cannot Be Declared `%s`', 'abstract', 'private');
+  }
+
+  public function process(XHPASTNode $root) {
+    $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
+
+    foreach ($methods as $method) {
+      $method_modifiers = $method
+        ->getChildOfType(0, 'n_METHOD_MODIFIER_LIST')
+        ->selectDescendantsOfType('n_STRING');
+      $modifiers = array();
+
+      foreach ($method_modifiers as $modifier) {
+        $modifiers[strtolower($modifier->getConcreteString())] = true;
+      }
+
+      if (idx($modifiers, 'abstract') && idx($modifiers, 'private')) {
+        $this->raiseLintAtNode(
+          $method,
+          pht(
+            '`%s` method cannot be declared `%s`. '.
+            'This construct will cause a fatal error.',
+            'abstract',
+            'private'));
+      }
+    }
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..127149d
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistAbstractPrivateMethodXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(
+        dirname(__FILE__).'/abstract-private-method/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/abstract-private-method/abstract-private-method.lint-test b/src/lint/linter/xhpast/rules/__tests__/abstract-private-method/abstract-private-method.lint-test
new file mode 100644 (file)
index 0000000..ea96208
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+abstract class SomeClass {
+  private abstract function someMethod();
+}
+~~~~~~~~~~
+error:3:3