-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Handle ClassConstFetch on RemoveUnusedAliasRector (#785)
* [CodingStyle] Handle CClassConstFetch on RemoveUnusedAliasRector * add conflict fixture * eol * add FullyQualifiedFromUseFinder * [ci-review] Rector Rectify * final touch: match -> matchAlias naming method * final touch: matchAlias -> matchAliasNamespace naming method Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
dd49d38
commit 13bf954
Showing
7 changed files
with
130 additions
and
40 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
23 changes: 23 additions & 0 deletions
23
...Style/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_has_conflict_class_contant.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,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture; | ||
|
||
use App\Bar as BarAlias; | ||
use App\Foo\Bar; | ||
use App\NoAlias; | ||
|
||
class SkipHasConflictClassConstant | ||
{ | ||
private $classMap = [ | ||
BarAlias::class => 'bar', | ||
NoAlias::class => 'noalias', | ||
]; | ||
|
||
public function run() | ||
{ | ||
Bar::execute(); | ||
Bar::DATA; | ||
} | ||
} | ||
|
||
?> |
12 changes: 0 additions & 12 deletions
12
...dingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/skip_used_in_class_constant.php.inc
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
...ts/CodingStyle/Rector/Use_/RemoveUnusedAliasRector/Fixture/used_in_class_constant.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,45 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture; | ||
|
||
use App\Bar as BarAlias; | ||
use App\NoAlias; | ||
|
||
class UsedInClassConstant | ||
{ | ||
private $classMap = [ | ||
BarAlias::class => 'bar', | ||
NoAlias::class => 'noalias', | ||
]; | ||
|
||
public function run() | ||
{ | ||
BarAlias::execute(); | ||
BarAlias::DATA; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture; | ||
|
||
use App\Bar; | ||
use App\NoAlias; | ||
|
||
class UsedInClassConstant | ||
{ | ||
private $classMap = [ | ||
Bar::class => 'bar', | ||
NoAlias::class => 'noalias', | ||
]; | ||
|
||
public function run() | ||
{ | ||
Bar::execute(); | ||
Bar::DATA; | ||
} | ||
} | ||
|
||
?> |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\PhpParser\NodeFinder; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Use_; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class FullyQualifiedFromUseFinder | ||
{ | ||
public function __construct( | ||
private BetterNodeFinder $betterNodeFinder | ||
) { | ||
} | ||
|
||
public function matchAliasNamespace(Use_ $use, string $loweredAliasName): ?Node | ||
{ | ||
return $this->betterNodeFinder->findFirstNext($use, function (Node $node) use ($loweredAliasName): bool { | ||
if (! $node instanceof FullyQualified) { | ||
return false; | ||
} | ||
|
||
$originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME); | ||
if (! $originalName instanceof Name) { | ||
return false; | ||
} | ||
|
||
$loweredOriginalName = strtolower($originalName->toString()); | ||
$loweredOriginalNameNamespace = Strings::before($loweredOriginalName, '\\'); | ||
return $loweredAliasName === $loweredOriginalNameNamespace; | ||
}); | ||
} | ||
} |