mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
[CodingStyle] Use alias name when exists on CatchExceptionNameMatchingTypeRector (#1920)
* [CodingStyle] Use alias name when exists on CatchExceptionNameMatchingTypeRector
* Fixed 🎉
* [ci-review] Rector Rectify
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
8d1ea5366f
commit
07df2bc028
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Catch_;
|
|||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Naming\Naming\AliasNameResolver;
|
||||
use Rector\Naming\Naming\PropertyNaming;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -30,7 +31,8 @@ final class CatchExceptionNameMatchingTypeRector extends AbstractRector
|
|||
private const STARTS_WITH_ABBREVIATION_REGEX = '#^([A-Za-z]+?)([A-Z]{1}[a-z]{1})([A-Za-z]*)#';
|
||||
|
||||
public function __construct(
|
||||
private readonly PropertyNaming $propertyNaming
|
||||
private readonly PropertyNaming $propertyNaming,
|
||||
private readonly AliasNameResolver $aliasNameResolver
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -101,6 +103,11 @@ CODE_SAMPLE
|
|||
$type = $node->types[0];
|
||||
$typeShortName = $this->nodeNameResolver->getShortName($type);
|
||||
|
||||
$aliasName = $this->aliasNameResolver->resolveByName($type);
|
||||
if (is_string($aliasName)) {
|
||||
$typeShortName = $aliasName;
|
||||
}
|
||||
|
||||
$newVariableName = Strings::replace(
|
||||
lcfirst($typeShortName),
|
||||
self::STARTS_WITH_ABBREVIATION_REGEX,
|
||||
|
|
50
rules/Naming/Naming/AliasNameResolver.php
Normal file
50
rules/Naming/Naming/AliasNameResolver.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user