Skip to content

Commit

Permalink
Remove PARENT_NODE from CatchExceptionNameMatchingTypeRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 22, 2023
1 parent 3c69a0f commit 2a7912d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
3 changes: 3 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\NodeTypeResolver\Node;

use PHPStan\Analyser\Scope;

/**
* @enum
*/
Expand All @@ -21,6 +23,7 @@ final class AttributeKey
public const VIRTUAL_NODE = 'virtual_node';

/**
* Contains @see Scope
* @var string
*/
public const SCOPE = 'scope';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 2a7912d

Please sign in to comment.