diff --git a/packages/NodeTypeResolver/Node/AttributeKey.php b/packages/NodeTypeResolver/Node/AttributeKey.php index 711c009e808..773f775d693 100644 --- a/packages/NodeTypeResolver/Node/AttributeKey.php +++ b/packages/NodeTypeResolver/Node/AttributeKey.php @@ -4,6 +4,8 @@ namespace Rector\NodeTypeResolver\Node; +use PHPStan\Analyser\Scope; + /** * @enum */ @@ -21,6 +23,7 @@ final class AttributeKey public const VIRTUAL_NODE = 'virtual_node'; /** + * Contains @see Scope * @var string */ public const SCOPE = 'scope'; diff --git a/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/skip_change_variable_next_jump_try.php.inc b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/skip_change_variable_next_jump_try.php.inc deleted file mode 100644 index 12d08f85707..00000000000 --- a/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/skip_change_variable_next_jump_try.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -verify($typoException); - $this->verify2($typoException); - } - } -} \ No newline at end of file diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index fcb60ada47a..3b68b6f4037 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -8,15 +8,12 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Catch_; -use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\TryCatch; use PHPStan\Analyser\Scope; use PHPStan\Type\ObjectType; use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; -use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Core\Rector\AbstractRector; use Rector\Naming\Naming\AliasNameResolver; use Rector\Naming\Naming\PropertyNaming; @@ -102,6 +99,12 @@ public function refactor(Node $node): ?Node continue; } + // variable defined first only resolvable by Scope pulled from Stmt + $scope = $stmt->getAttribute(AttributeKey::SCOPE); + if (! $scope instanceof Scope) { + continue; + } + /** @var TryCatch $stmt */ $catch = $stmt->catches[0]; @@ -128,12 +131,6 @@ public function refactor(Node $node): ?Node continue; } - // variable defined first only resolvable by Scope pulled from Stmt - $scope = $stmt->getAttribute(AttributeKey::SCOPE); - if (! $scope instanceof Scope) { - continue; - } - $isFoundInPrevious = $scope->hasVariableType($newVariableName) ->yes(); if ($isFoundInPrevious) { @@ -190,16 +187,7 @@ private function shouldSkip(Stmt $stmt): bool } $catch = $stmt->catches[0]; - if (! $catch->var instanceof Variable) { - return true; - } - - $parentNode = $stmt->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof FileWithoutNamespace || $parentNode instanceof Namespace_) { - return false; - } - - return ! $parentNode instanceof FunctionLike; + return ! $catch->var instanceof Variable; } /**