Skip to content

Commit

Permalink
Revert "[Performance][NodeTypeResolver] Remove $mutatingScope->enterC…
Browse files Browse the repository at this point in the history
…atchType() usage on PHPStanNodeScopeResolver (#5045)" (#5046)

This reverts commit 5d27404.
  • Loading branch information
samsonasik committed Sep 19, 2023
1 parent 5d27404 commit 535ef59
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@
use PHPStan\Analyser\ScopeContext;
use PHPStan\Node\UnreachableStatementNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PHPStan\NodeVisitor\ExprScopeFromStmtNodeVisitor;
use Rector\Core\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -83,6 +86,7 @@ public function __construct(
iterable $nodeVisitors,
private readonly ScopeFactory $scopeFactory,
private readonly PrivatesAccessor $privatesAccessor,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ClassAnalyzer $classAnalyzer
) {
$this->nodeTraverser = new NodeTraverser();
Expand Down Expand Up @@ -148,7 +152,7 @@ public function processNodes(
} elseif ($node instanceof Switch_) {
$this->processSwitch($node, $mutatingScope);
} elseif ($node instanceof TryCatch) {
$this->processTryCatch($node, $mutatingScope);
$this->processTryCatch($node, $filePath, $mutatingScope);
} elseif ($node instanceof ArrayItem) {
$this->processArrayItem($node, $mutatingScope);
} elseif ($node instanceof NullableType) {
Expand Down Expand Up @@ -267,13 +271,19 @@ private function processSwitch(Switch_ $switch, MutatingScope $mutatingScope): v
}
}

private function processTryCatch(TryCatch $tryCatch, MutatingScope $mutatingScope): void
private function processTryCatch(TryCatch $tryCatch, string $filePath, MutatingScope $mutatingScope): void
{
foreach ($tryCatch->catches as $catch) {
$catch->setAttribute(AttributeKey::SCOPE, $mutatingScope);
if ($catch->var instanceof Variable) {
$catch->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
$varName = $catch->var instanceof Variable
? $this->nodeNameResolver->getName($catch->var)
: null;

$type = TypeCombinator::union(
...array_map(static fn (Name $name): ObjectType => new ObjectType((string) $name), $catch->types)
);

$catchMutatingScope = $mutatingScope->enterCatchType($type, $varName);
$this->processNodes($catch->stmts, $filePath, $catchMutatingScope);
}

if ($tryCatch->finally instanceof Finally_) {
Expand Down

0 comments on commit 535ef59

Please sign in to comment.