Page MenuHomePhabricator

D14557.id35221.diff
No OneTemporary

D14557.id35221.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -347,6 +347,8 @@
'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 @@
'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
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistUnnecessarySymbolAliasXHPASTLinterRule.php
@@ -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
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase.php
@@ -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
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/unnecessary-symbol-alias/use.lint-test
@@ -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;

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 9, 7:57 PM (3 d, 4 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6716558
Default Alt Text
D14557.id35221.diff (4 KB)

Event Timeline