Skip to content
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
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d3f5d81
Add NoUnneededAliasFixer
JakeFr Aug 9, 2019
f25e471
Make NoUnneededAliasFixer final
JakeFr Aug 9, 2019
fe4dabd
Rename to NoUnneededImportAliasFixer, supports comments, minor fixes …
JakeFr Aug 12, 2019
c230285
Add support for multiple use statement in NamespaceUsesAnalyzer
JakeFr Aug 12, 2019
0fb2227
Improve isCandidate
JakeFr Aug 13, 2019
04203fe
Merge branch 'master' into nounneededalias
keradus Aug 24, 2019
3dcd82d
Add in PhpCsFixer rules sets, add code sample for function and const …
JakeFr Sep 24, 2019
c2619ae
Merge branch 'master' into nounneededalias
JakeFr Sep 24, 2019
b694388
Code sample is not specific
JakeFr Sep 25, 2019
0dcf935
Apply php-cs-fixer
JakeFr Sep 25, 2019
1bc7abd
Fix test with close tags
JakeFr Sep 25, 2019
d23a138
Add NoUnneededAliasFixer
JakeFr Aug 9, 2019
9145bcd
Make NoUnneededAliasFixer final
JakeFr Aug 9, 2019
660e566
Rename to NoUnneededImportAliasFixer, supports comments, minor fixes …
JakeFr Aug 12, 2019
f74a41b
Add support for multiple use statement in NamespaceUsesAnalyzer
JakeFr Aug 12, 2019
7565700
Improve isCandidate
JakeFr Aug 13, 2019
af14d5a
Add in PhpCsFixer rules sets, add code sample for function and const …
JakeFr Sep 24, 2019
a374577
Code sample is not specific
JakeFr Sep 25, 2019
6a0ab72
Apply php-cs-fixer
JakeFr Sep 25, 2019
0547f98
Fix test with close tags
JakeFr Sep 25, 2019
c5d6da9
Merge branch 'nounneededalias' of https://github.com/JakeFr/PHP-CS-Fi…
JakeFr Oct 9, 2019
df50327
Merge branch 'master' into nounneededalias
JakeFr Oct 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -1139,6 +1139,10 @@ Choose from the list of available rules:

There MUST be no trailing spaces inside comment or PHPDoc.

* **no_unneeded_alias**
JakeFr marked this conversation as resolved.
Show resolved Hide resolved

Remove unneeded alias in ``use`` clauses.

* **no_unneeded_control_parentheses** [@Symfony, @PhpCsFixer]

Removes unneeded parentheses around control statements.
Expand Down
93 changes: 93 additions & 0 deletions src/Fixer/Import/NoUnneededAliasFixer.php
@@ -0,0 +1,93 @@
<?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 NoUnneededAliasFixer extends AbstractFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition()
{
return new FixerDefinition(
'Remove unneeded alias in `use` clauses.',
[new CodeSample("<?php\nnamespace Foo;\nuse Bar\\Baz as Baz;\n")]
);
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
// should be run after the SingleImportPerStatementFixer (for fix separated use statements as well) and NoUnusedImportsFixer (just for save performance) and NoLeadingImportSlashFixer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this new fixer should be able to deal with grouped imports in case single_import_per_statement is not enabled. This would remove the priority issue. Same with no_leading_import_slash.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly NamespaceUsesAnalyzer skips group and multiple use

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add support for multiple use (not group) with an extra argument to avoid side effect

return -25;
}

/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_USE);
}

/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens);

if (0 === \count($useDeclarations)) {
JakeFr marked this conversation as resolved.
Show resolved Hide resolved
return;
}

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)
{
// no fix if any comment found.
julienfalque marked this conversation as resolved.
Show resolved Hide resolved
$commentIndex = $tokens->getNextTokenOfKind($declaration->getStartIndex(), [[T_COMMENT], [T_DOC_COMMENT]]);
if (null !== $commentIndex && $commentIndex <= $declaration->getEndIndex()) {
return;
}

$asIndex = $tokens->getNextTokenOfKind($declaration->getStartIndex(), [[T_AS]]);
if (null === $asIndex || $asIndex > $declaration->getEndIndex()) {
return;
}

$tokens->clearRange($tokens->getPrevMeaningfulToken($asIndex) + 1, $declaration->getEndIndex() - 1);
}
}
92 changes: 92 additions & 0 deletions tests/Fixer/Import/NoUnneededAliasFixerTest.php
@@ -0,0 +1,92 @@
<?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\Tests\Fixer\Import;

use PhpCsFixer\Tests\Test\AbstractFixerTestCase;

/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Import\NoUnneededAliasFixer
*/
final class NoUnneededAliasFixerTest extends AbstractFixerTestCase
{
/**
* @param string $expected
* @param null|string $input
*
* @dataProvider provideFixCases
*/
public function testFix($expected, $input = null)
{
$this->doTest($expected, $input);
}

public function provideFixCases()
{
return [
'simple' => [
<<<'EOF'
<?php

use Foo\Bar\FooBar;
use const some\a\ConstA;
use function some\a\fn_b;
EOF
,
<<<'EOF'
<?php

use Foo\Bar\FooBar as FooBar;
use const some\a\ConstA as ConstA;
use function some\a\fn_b as fn_b;
EOF
],
'comments' => [
<<<'EOF'
<?php

use Foo\Bar\FooBar as /* idem */FooBar;
use /* - */const some\a\ConstA as ConstA;
use function some\a\fn_b /* - */as fn_b;
EOF
],
'case sensitive' => [
<<<'EOF'
<?php

use Foo\Bar\FooBar as fooBar;
use const some\a\ConstA as Consta;
use function some\a\fn_b as fn_B;
EOF
],
'extra spaces' => [
<<<'EOF'
<?php

use Foo\Bar\FooBar;
use const some\a\ConstA;
use function some\a\fn_b;
EOF
,
<<<'EOF'
<?php

use Foo\Bar\FooBar as FooBar ;
use const some\a\ConstA as ConstA ;
use function some\a\fn_b as fn_b ;
EOF
],
];
}
}