From cb5b4dd8bfe1280d92836f1e29335c26097a38f5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 Jul 2023 17:43:32 +0700 Subject: [PATCH 1/2] [PostRector] Reduce repetitive resolve uses statements on NameImportingPostRector --- packages/PostRector/Rector/NameImportingPostRector.php | 2 +- .../Catch_/CatchExceptionNameMatchingTypeRector.php | 8 ++++++-- rules/Naming/Naming/AliasNameResolver.php | 8 ++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/PostRector/Rector/NameImportingPostRector.php b/packages/PostRector/Rector/NameImportingPostRector.php index 1fdd248046e..7bfb872e559 100644 --- a/packages/PostRector/Rector/NameImportingPostRector.php +++ b/packages/PostRector/Rector/NameImportingPostRector.php @@ -149,7 +149,7 @@ private function resolveNameInUse(Name $name, array $currentUses): null|Name|Ful return null; } - $aliasName = $this->aliasNameResolver->resolveByName($name); + $aliasName = $this->aliasNameResolver->resolveByName($name, $currentUses); if (is_string($aliasName)) { return new Name($aliasName); } diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 62a77fd038d..70f4408d3ac 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -22,6 +22,7 @@ use Rector\Core\Rector\AbstractRector; use Rector\Naming\Naming\AliasNameResolver; use Rector\Naming\Naming\PropertyNaming; +use Rector\Naming\Naming\UseImportsResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -39,7 +40,8 @@ final class CatchExceptionNameMatchingTypeRector extends AbstractRector public function __construct( private readonly PropertyNaming $propertyNaming, - private readonly AliasNameResolver $aliasNameResolver + private readonly AliasNameResolver $aliasNameResolver, + private readonly UseImportsResolver $useImportsResolver ) { } @@ -105,6 +107,8 @@ public function refactor(Node $node): ?Node } $hasChanged = false; + $uses = $this->useImportsResolver->resolve(); + foreach ($node->stmts as $key => $stmt) { if ($this->shouldSkip($stmt)) { continue; @@ -127,8 +131,8 @@ public function refactor(Node $node): ?Node $type = $catch->types[0]; $typeShortName = $this->nodeNameResolver->getShortName($type); + $aliasName = $this->aliasNameResolver->resolveByName($type, $uses); - $aliasName = $this->aliasNameResolver->resolveByName($type); if (is_string($aliasName)) { $typeShortName = $aliasName; } diff --git a/rules/Naming/Naming/AliasNameResolver.php b/rules/Naming/Naming/AliasNameResolver.php index 44a5b886dac..57cc495b273 100644 --- a/rules/Naming/Naming/AliasNameResolver.php +++ b/rules/Naming/Naming/AliasNameResolver.php @@ -6,6 +6,8 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; +use PhpParser\Node\Stmt\GroupUse; +use PhpParser\Node\Stmt\Use_; final class AliasNameResolver { @@ -14,9 +16,11 @@ public function __construct( ) { } - public function resolveByName(Name $name): ?string + /** + * @param Use_[]|GroupUse[] $uses + */ + public function resolveByName(Name $name, array $uses): ?string { - $uses = $this->useImportsResolver->resolve(); $nameString = $name->toString(); foreach ($uses as $use) { From 913515d3c466a13d2bb3a89910e6e7c3d42f2dfd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 22 Jul 2023 17:44:51 +0700 Subject: [PATCH 2/2] [PostRector] Reduce repetitive resolve uses statements on NameImportingPostRector --- .../Rector/Catch_/CatchExceptionNameMatchingTypeRector.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 70f4408d3ac..2147d2ab5ed 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node } $hasChanged = false; - $uses = $this->useImportsResolver->resolve(); + $uses = null; foreach ($node->stmts as $key => $stmt) { if ($this->shouldSkip($stmt)) { @@ -129,6 +129,10 @@ public function refactor(Node $node): ?Node /** @var string $oldVariableName */ $oldVariableName = (string) $this->getName($catchVar); + if ($uses === null) { + $uses = $this->useImportsResolver->resolve(); + } + $type = $catch->types[0]; $typeShortName = $this->nodeNameResolver->getShortName($type); $aliasName = $this->aliasNameResolver->resolveByName($type, $uses);