]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for unnecessary symbol aliases
authorJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:17:25 +0000 (08:17 +1100)
committerJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 21:17:25 +0000 (08:17 +1100)
Summary: Add a linter rule to detect symbols which are aliased with the same name, such as `use X\Y\Z as Z`. This is unnecessary and is more-simply expressed as `use X\Y\Z`.

Test Plan: Added unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test [new file with mode: 0644]

index 0dee95d0c101d4615aad6051c6f575638231f7c5..40d0bfb75427305eb89718e3673c9e766de3fdbf 100644 (file)
@@ -347,6 +347,8 @@ phutil_register_library_map(array(
     'ArcanistUnnecessaryFinalModifierXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessaryFinalModifierXHPASTLinterRule.php',
     'ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase.php',
     'ArcanistUnnecessarySemicolonXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessarySemicolonXHPASTLinterRule.php',
+    'ArcanistUnnecessarySymbolAliasXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php',
+    'ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php',
     'ArcanistUnsafeDynamicStringXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnsafeDynamicStringXHPASTLinterRule.php',
     'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase.php',
     'ArcanistUpgradeWorkflow' => 'workflow/ArcanistUpgradeWorkflow.php',
@@ -727,6 +729,8 @@ phutil_register_library_map(array(
     'ArcanistUnnecessaryFinalModifierXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUnnecessaryFinalModifierXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistUnnecessarySemicolonXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistUnnecessarySymbolAliasXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistUnsafeDynamicStringXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUnsafeDynamicStringXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistUpgradeWorkflow' => 'ArcanistWorkflow',
diff --git a/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..f2b1fb0
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+final class ArcanistUnnecessarySymbolAliasXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 99;
+
+  public function getLintName() {
+    return pht('Unnecessary Symbol Alias');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $uses = $root->selectDescendantsOfType('n_USE');
+
+    foreach ($uses as $use) {
+      $symbol = $use->getChildOfType(0, 'n_SYMBOL_NAME');
+      $alias  = $use->getChildByIndex(1);
+
+      if ($alias->getTypeName() == 'n_EMPTY') {
+        continue;
+      }
+
+      $symbol_name = last(explode('\\', $symbol->getConcreteString()));
+      $alias_name  = $alias->getConcreteString();
+
+      if ($symbol_name == $alias_name) {
+        $this->raiseLintAtNode(
+          $use,
+          pht(
+            'Importing `%s` with `%s` is unnecessary because the aliased '.
+            'name is identical to the imported symbol name.',
+            $symbol->getConcreteString(),
+            sprintf('as %s', $alias_name)),
+          $symbol->getConcreteString());
+      }
+    }
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..21ce268
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(
+      dirname(__FILE__).'/unnecessary-symbol-alias/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test b/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test
new file mode 100644 (file)
index 0000000..a9a25d5
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+use A;
+use B as B;
+use C as D;
+use E\F as F;
+use G\H as I;
+~~~~~~~~~~
+warning:3:5
+warning:5:5
+~~~~~~~~~~
+<?php
+use A;
+use B;
+use C as D;
+use E\F;
+use G\H as I;