-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Add RemoveUselessAliasInUseStatementRector (#5394)
* [CodingStyle] Add RemoveUselessAliasInUseStatementRector * udpat edocs * final touch: add test for different alias with name * final touch: no namespace useless alias * final touch: phpstan * really final touch: fix phpstan * fix
- Loading branch information
1 parent
6bff511
commit 6646d23
Showing
12 changed files
with
205 additions
and
6 deletions.
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
11 changes: 11 additions & 0 deletions
11
...or/Stmt/RemoveUselessAliasInUseStatementRector/Fixture/no_namespace_useless_alias.php.inc
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,11 @@ | ||
<?php | ||
|
||
use Doctrine\ORM\Mapping as Mapping; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use Doctrine\ORM\Mapping; | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...e/Rector/Stmt/RemoveUselessAliasInUseStatementRector/Fixture/remove_useless_alias.php.inc
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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use stdClass as stdClass; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
.../Rector/Stmt/RemoveUselessAliasInUseStatementRector/Fixture/remove_useless_alias2.php.inc
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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as Mapping; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping; | ||
|
||
?> |
5 changes: 5 additions & 0 deletions
5
...RemoveUselessAliasInUseStatementRector/Fixture/skip_alias_different_with_lastname.php.inc
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,5 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use stdClass as Foo; |
5 changes: 5 additions & 0 deletions
5
...emoveUselessAliasInUseStatementRector/Fixture/skip_alias_different_with_lastname2.php.inc
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,5 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use Doctrine\ORM\Mapping as ORM; |
5 changes: 5 additions & 0 deletions
5
...ingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector/Fixture/skip_no_alias.php.inc
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,5 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture; | ||
|
||
use stdClass; |
28 changes: 28 additions & 0 deletions
28
...tmt/RemoveUselessAliasInUseStatementRector/RemoveUselessAliasInUseStatementRectorTest.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveUselessAliasInUseStatementRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector/config/configured_rule.php
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveUselessAliasInUseStatementRector::class); | ||
}; |
89 changes: 89 additions & 0 deletions
89
rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Rector\Stmt; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\Node\Stmt\Use_; | ||
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\RemoveUselessAliasInUseStatementRectorTest | ||
*/ | ||
final class RemoveUselessAliasInUseStatementRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Remove useless alias in use statement as same name with last use statement name', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use App\Bar as Bar; | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use App\Bar; | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FileWithoutNamespace::class, Namespace_::class]; | ||
} | ||
|
||
/** | ||
* @param FileWithoutNamespace|Namespace_ $node | ||
*/ | ||
public function refactor(Node $node): null|FileWithoutNamespace|Namespace_ | ||
{ | ||
$hasChanged = false; | ||
foreach ($node->stmts as $stmt) { | ||
if (! $stmt instanceof Use_) { | ||
continue; | ||
} | ||
|
||
if (count($stmt->uses) !== 1) { | ||
continue; | ||
} | ||
|
||
if (! isset($stmt->uses[0])) { | ||
continue; | ||
} | ||
|
||
$aliasName = $stmt->uses[0]->alias instanceof Identifier | ||
? $stmt->uses[0]->alias->toString() | ||
: null; | ||
|
||
if ($aliasName === null) { | ||
continue; | ||
} | ||
|
||
$useName = $stmt->uses[0]->name->toString(); | ||
$lastName = Strings::after($useName, '\\', -1) ?? $useName; | ||
if ($lastName === $aliasName) { | ||
$stmt->uses[0]->alias = null; | ||
$hasChanged = true; | ||
} | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
} |