Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #6267 NoUnneededImportAliasFixer - Introduction (SpacePossum)
This PR was merged into the master branch. Discussion ---------- NoUnneededImportAliasFixer - Introduction replaces #4498 Commits ------- 8ea666d NoUnneededImportAliasFixer - Introduction
- Loading branch information
Showing
10 changed files
with
269 additions
and
1 deletion.
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
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,30 @@ | ||
================================= | ||
Rule ``no_unneeded_import_alias`` | ||
================================= | ||
|
||
Imports should not be aliased as the same name. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
-use A\B\Foo as Foo; | ||
+use A\B\Foo ; | ||
Rule sets | ||
--------- | ||
|
||
The rule is part of the following rule sets: | ||
|
||
@PhpCsFixer | ||
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_unneeded_import_alias`` rule. | ||
|
||
@Symfony | ||
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_unneeded_import_alias`` rule. |
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,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoUnneededImportAliasFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Imports should not be aliased as the same name.', | ||
[new CodeSample("<?php\nuse A\\B\\Foo as Foo;\n")] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAllTokenKindsFound([T_USE, T_AS]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before NoSinglelineWhitespaceBeforeSemicolonsFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = \count($tokens) - 1; 0 <= $index; --$index) { | ||
if (!$tokens[$index]->isGivenKind(T_AS)) { | ||
continue; | ||
} | ||
|
||
$aliasIndex = $tokens->getNextMeaningfulToken($index); | ||
|
||
if (!$tokens[$aliasIndex]->isGivenKind(T_STRING)) { | ||
continue; | ||
} | ||
|
||
$importIndex = $tokens->getPrevMeaningfulToken($index); | ||
|
||
if (!$tokens[$importIndex]->isGivenKind(T_STRING)) { | ||
continue; | ||
} | ||
|
||
if ($tokens[$importIndex]->getContent() !== $tokens[$aliasIndex]->getContent()) { | ||
continue; | ||
} | ||
|
||
do { | ||
$importIndex = $tokens->getPrevMeaningfulToken($importIndex); | ||
} while ($tokens[$importIndex]->isGivenKind([T_NS_SEPARATOR, T_STRING, T_AS]) || $tokens[$importIndex]->equals(',')); | ||
|
||
if ($tokens[$importIndex]->isGivenKind([CT::T_FUNCTION_IMPORT, CT::T_CONST_IMPORT])) { | ||
$importIndex = $tokens->getPrevMeaningfulToken($importIndex); | ||
} | ||
|
||
if (!$tokens[$importIndex]->isGivenKind([T_USE, CT::T_GROUP_IMPORT_BRACE_OPEN])) { | ||
continue; | ||
} | ||
|
||
$tokens->clearTokenAndMergeSurroundingWhitespace($aliasIndex); | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($index); | ||
} | ||
} | ||
} |
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
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,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\NoUnneededImportAliasFixer | ||
*/ | ||
final class NoUnneededImportAliasFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFix(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideFixCases(): iterable | ||
{ | ||
yield [ | ||
'<?php use some\ns\{ClassA, ClassB, ClassC };', | ||
'<?php use some\ns\{ClassA, ClassB, ClassC as ClassC};', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
use A\B\C , D\E\F as G; | ||
use const X\Y\Z as Z1, U\V\W ; | ||
use function U\V\W\FX , U\V\W\FY ; | ||
', | ||
'<?php | ||
use A\B\C as C, D\E\F as G; | ||
use const X\Y\Z as Z1, U\V\W as W; | ||
use function U\V\W\FX as FX, U\V\W\FY as FY; | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
use F ; | ||
use X as x; | ||
use const CA ; | ||
use function FW ; | ||
', | ||
'<?php | ||
use F as F; | ||
use X as x; | ||
use const CA as CA; | ||
use function FW as FW; | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
use /* 1 */\F ; | ||
use const \CA/* 2 */ /* 3 */; | ||
use /* 4 */ function/* 5 */ \FW /* 6 */ /* 7 */ ; | ||
', | ||
'<?php | ||
use /* 1 */\F as F; | ||
use const \CA/* 2 */ as CA/* 3 */; | ||
use /* 4 */ function/* 5 */ \FW /* 6 */ as /* 7 */ FW; | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
use \F\B\C ; | ||
use const \X\Y\CA ; | ||
use function \U\V\FW ; | ||
', | ||
'<?php | ||
use \F\B\C as C; | ||
use const \X\Y\CA as CA; | ||
use function \U\V\FW as FW; | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php use A\B ?> X <?php use C\D; use E\F ?>', | ||
'<?php use A\B as B ?> X <?php use C\D; use E\F as F ?>', | ||
]; | ||
|
||
yield [ | ||
'<?php use A\B ?>', | ||
'<?php use A\B as B ?>', | ||
]; | ||
|
||
yield [ | ||
'<?php foreach ($a as $a) {}', | ||
]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...gration/priority/no_unneeded_import_alias,no_singleline_whitespace_before_semicolons.test
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,17 @@ | ||
--TEST-- | ||
Integration of fixers: no_unneeded_import_alias,no_singleline_whitespace_before_semicolons. | ||
--RULESET-- | ||
{"no_unneeded_import_alias": true, "no_singleline_whitespace_before_semicolons": true} | ||
--EXPECT-- | ||
<?php | ||
|
||
use function U\V\W\FY; | ||
|
||
FY(); | ||
|
||
--INPUT-- | ||
<?php | ||
|
||
use function U\V\W\FY as FY; | ||
|
||
FY(); |