]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for public properties
authorJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 22:48:35 +0000 (09:48 +1100)
committerJoshua Spence <josh@freelancer.com>
Wed, 2 Dec 2015 22:48:48 +0000 (09:48 +1100)
Summary: Add a linter rule which advises against public class properties.

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
src/lint/linter/xhpast/rules/ArcanistPublicPropertyXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistPublicPropertyXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/public-property/class.lint-test [new file with mode: 0644]

index 9c9e09edbeb045472307d5417368fea8ee359773..4d1a81c606b8258a2334d41d01addd035df9cadd 100644 (file)
@@ -290,6 +290,8 @@ phutil_register_library_map(array(
     'ArcanistPlusOperatorOnStringsXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistPlusOperatorOnStringsXHPASTLinterRuleTestCase.php',
     'ArcanistPregQuoteMisuseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistPregQuoteMisuseXHPASTLinterRule.php',
     'ArcanistPregQuoteMisuseXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistPregQuoteMisuseXHPASTLinterRuleTestCase.php',
+    'ArcanistPublicPropertyXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistPublicPropertyXHPASTLinterRule.php',
+    'ArcanistPublicPropertyXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistPublicPropertyXHPASTLinterRuleTestCase.php',
     'ArcanistPuppetLintLinter' => 'lint/linter/ArcanistPuppetLintLinter.php',
     'ArcanistPuppetLintLinterTestCase' => 'lint/linter/__tests__/ArcanistPuppetLintLinterTestCase.php',
     'ArcanistPyFlakesLinter' => 'lint/linter/ArcanistPyFlakesLinter.php',
@@ -694,6 +696,8 @@ phutil_register_library_map(array(
     'ArcanistPlusOperatorOnStringsXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistPregQuoteMisuseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistPregQuoteMisuseXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+    'ArcanistPublicPropertyXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistPublicPropertyXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistPuppetLintLinter' => 'ArcanistExternalLinter',
     'ArcanistPuppetLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
     'ArcanistPyFlakesLinter' => 'ArcanistExternalLinter',
index f780ec6cfdd91d9d29a7f3ca40522325d97984ed..d6828e2899acd2285f3bb3a54867b02f5dfc4e6d 100644 (file)
@@ -237,6 +237,7 @@ abstract class ArcanistXHPASTLinterRule extends Phobject {
 
     switch ($modifier_list->getTypeName()) {
       case 'n_CLASS_ATTRIBUTES':
+      case 'n_CLASS_MEMBER_MODIFIER_LIST':
       case 'n_METHOD_MODIFIER_LIST':
         break;
 
diff --git a/src/lint/linter/xhpast/rules/ArcanistPublicPropertyXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistPublicPropertyXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..da07ef1
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+final class ArcanistPublicPropertyXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 130;
+
+  public function getLintName() {
+    return pht('Use of `%s` Properties', 'public');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_ADVICE;
+  }
+
+  public function process(XHPASTNode $root) {
+    $members = $root->selectDescendantsOfType(
+      'n_CLASS_MEMBER_DECLARATION_LIST');
+
+    foreach ($members as $member) {
+      $modifiers = $this->getModifiers($member);
+
+      if (isset($modifiers['public'])) {
+        $this->raiseLintAtNode(
+          $member,
+          pht(
+            '`%s` properties should be avoided. Instead of exposing '.
+            'the property value directly, consider using getter '.
+            'and setter methods.',
+            'public'));
+      }
+    }
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistPublicPropertyXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistPublicPropertyXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..35e4d5f
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+final class ArcanistPublicPropertyXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(dirname(__FILE__).'/public-property/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/public-property/class.lint-test b/src/lint/linter/xhpast/rules/__tests__/public-property/class.lint-test
new file mode 100644 (file)
index 0000000..a7921af
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+class SomeClass {
+  private $x;
+  protected $y;
+  public $z;
+}
+~~~~~~~~~~
+advice:6:3