mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-29 10:12:41 +01:00
Add a linter rule for unnecessary symbol aliases
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
This commit is contained in:
parent
3b41e62f87
commit
6f908f633b
4 changed files with 74 additions and 0 deletions
|
@ -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',
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
final class ArcanistUnnecessarySymbolAliasXHPASTLinterRuleTestCase
|
||||
extends ArcanistXHPASTLinterRuleTestCase {
|
||||
|
||||
public function testLinter() {
|
||||
$this->executeTestsInDirectory(
|
||||
dirname(__FILE__).'/unnecessary-symbol-alias/');
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
Loading…
Reference in a new issue