-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Use alias name when exists on CatchExceptionNameMatchin…
…gTypeRector (#1920) * [CodingStyle] Use alias name when exists on CatchExceptionNameMatchingTypeRector * Fixed 🎉 * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
8d1ea53
commit 07df2bc
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
.../CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/use_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,39 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture; | ||
|
||
use Exception as SomeException; | ||
|
||
class UseAlias | ||
{ | ||
public function run() | ||
{ | ||
try { | ||
// ... | ||
} catch (SomeException $typoException) { | ||
$typoException->getMessage(); | ||
} | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture; | ||
|
||
use Exception as SomeException; | ||
|
||
class UseAlias | ||
{ | ||
public function run() | ||
{ | ||
try { | ||
// ... | ||
} catch (SomeException $someException) { | ||
$someException->getMessage(); | ||
} | ||
} | ||
} | ||
|
||
?> |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Naming\Naming; | ||
|
||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Use_; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class AliasNameResolver | ||
{ | ||
public function __construct( | ||
private readonly NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function resolveByName(Name $name): ?string | ||
{ | ||
/** @var Use_[] $useNodes */ | ||
$useNodes = $name->getAttribute(AttributeKey::USE_NODES); | ||
$nameString = $name->toString(); | ||
|
||
foreach ($useNodes as $useNode) { | ||
$useUses = $useNode->uses; | ||
if (count($useUses) > 1) { | ||
continue; | ||
} | ||
|
||
if (! isset($useUses[0])) { | ||
continue; | ||
} | ||
|
||
$useUse = $useUses[0]; | ||
if (! $useUse->alias instanceof Identifier) { | ||
continue; | ||
} | ||
|
||
if (! $this->nodeNameResolver->isName($useUse->name, $nameString)) { | ||
continue; | ||
} | ||
|
||
return (string) $useUse->getAlias(); | ||
} | ||
|
||
return null; | ||
} | ||
} |