New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NoUnneededImportAliasFixer #4498
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d3f5d81
Add NoUnneededAliasFixer
JakeFr f25e471
Make NoUnneededAliasFixer final
JakeFr fe4dabd
Rename to NoUnneededImportAliasFixer, supports comments, minor fixes …
JakeFr c230285
Add support for multiple use statement in NamespaceUsesAnalyzer
JakeFr 0fb2227
Improve isCandidate
JakeFr 04203fe
Merge branch 'master' into nounneededalias
keradus 3dcd82d
Add in PhpCsFixer rules sets, add code sample for function and const …
JakeFr c2619ae
Merge branch 'master' into nounneededalias
JakeFr b694388
Code sample is not specific
JakeFr 0dcf935
Apply php-cs-fixer
JakeFr 1bc7abd
Fix test with close tags
JakeFr d23a138
Add NoUnneededAliasFixer
JakeFr 9145bcd
Make NoUnneededAliasFixer final
JakeFr 660e566
Rename to NoUnneededImportAliasFixer, supports comments, minor fixes …
JakeFr f74a41b
Add support for multiple use statement in NamespaceUsesAnalyzer
JakeFr 7565700
Improve isCandidate
JakeFr af14d5a
Add in PhpCsFixer rules sets, add code sample for function and const …
JakeFr a374577
Code sample is not specific
JakeFr 6a0ab72
Apply php-cs-fixer
JakeFr 0547f98
Fix test with close tags
JakeFr c5d6da9
Merge branch 'nounneededalias' of https://github.com/JakeFr/PHP-CS-Fi…
JakeFr df50327
Merge branch 'master' into nounneededalias
JakeFr File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\Import; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis; | ||
use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoUnneededImportAliasFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition() | ||
{ | ||
return new FixerDefinition( | ||
'Remove unneeded alias in `use` clauses.', | ||
[ | ||
new CodeSample( | ||
'<?php | ||
namespace Foo; | ||
|
||
use Bar\Baz as Baz; | ||
|
||
use const some\a\ConstA as ConstA; | ||
use function some\a\fn_b as fn_b; | ||
' | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPriority() | ||
{ | ||
// should be run after the NoUnusedImportsFixer (just for save performance) | ||
return -15; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_USE, T_AS]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
$useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens, false); | ||
|
||
foreach ($useDeclarations as $declaration) { | ||
if (!$declaration->isAliased()) { | ||
continue; | ||
} | ||
$shortNameStartPos = strrpos($declaration->getFullName(), '\\'); | ||
$shortName = false === $shortNameStartPos ? $declaration->getFullName() : substr($declaration->getFullName(), $shortNameStartPos + 1); | ||
|
||
if ($declaration->getShortName() !== $shortName) { | ||
continue; | ||
} | ||
|
||
$this->removeAlias($tokens, $declaration); | ||
} | ||
} | ||
|
||
private function removeAlias(Tokens $tokens, NamespaceUseAnalysis $declaration) | ||
{ | ||
$asIndex = $tokens->getNextTokenOfKind($declaration->getStartIndex(), [[T_AS]]); | ||
if (null === $asIndex || $asIndex > $declaration->getEndIndex()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is only called if the import uses an alias so this should never happen. |
||
return; | ||
} | ||
|
||
for ($i = $tokens->getPrevMeaningfulToken($asIndex) + 1; $i <= $declaration->getEndIndex() - 1; ++$i) { | ||
if ($tokens[$i]->isWhitespace() && !($tokens[$i - 1]->isComment() || $tokens[$i + 1]->isComment() || $tokens[$i + 1]->isGivenKind([T_CLOSE_TAG]))) { | ||
$tokens->clearAt($i); | ||
} elseif ($tokens[$i]->isGivenKind([T_AS, T_STRING])) { | ||
$tokens->clearAt($i); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading this sentence, i don't know what
unneeded
means.is
baz as baz
only case? then, audience of this rule is very limitedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's unneeded when the alias is equal to the last part of the fqcn
same with function and const imports
I found some import in big legacy project
not found in recent projects with modern IDE