Skip to content

Commit

Permalink
[NodeTypeResolver] No need to resolve class name on anonymous classes (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 16, 2024
1 parent db213ee commit 16d1df9
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
use PHPStan\Analyser\ScopeContext;
use PHPStan\Node\UnreachableStatementNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -228,7 +228,7 @@ private function nodeScopeResolverProcessNodes(
): void {
try {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
} catch (\PHPStan\ShouldNotHappenException) {
} catch (ShouldNotHappenException) {
}
}

Expand Down Expand Up @@ -361,29 +361,31 @@ private function resolveClassOrInterfaceScope(
Class_ | Interface_ | Enum_ $classLike,
MutatingScope $mutatingScope
): MutatingScope {
$className = $this->resolveClassName($classLike);
$isAnonymous = $this->classAnalyzer->isAnonymousClass($classLike);

// is anonymous class? - not possible to enter it since PHPStan 0.12.33, see https://github.com/phpstan/phpstan-src/commit/e87fb0ec26f9c8552bbeef26a868b1e5d8185e91
if ($classLike instanceof Class_ && $isAnonymous) {
$classReflection = $this->reflectionProvider->getAnonymousClassReflection($classLike, $mutatingScope);
} elseif (! $this->reflectionProvider->hasClass($className)) {
return $mutatingScope;
} else {
$className = $this->resolveClassName($classLike);
if (! $this->reflectionProvider->hasClass($className)) {
return $mutatingScope;
}

$classReflection = $this->reflectionProvider->getClass($className);
}

try {
return $mutatingScope->enterClass($classReflection);
} catch (\PHPStan\ShouldNotHappenException) {
} catch (ShouldNotHappenException) {
}

$context = $this->privatesAccessor->getPrivateProperty($mutatingScope, 'context');
$this->privatesAccessor->setPrivateProperty($context, 'classReflection', null);

try {
return $mutatingScope->enterClass($classReflection);
} catch (\PHPStan\ShouldNotHappenException) {
} catch (ShouldNotHappenException) {
}

return $mutatingScope;
Expand All @@ -396,7 +398,7 @@ private function resolveClassName(Class_ | Interface_ | Trait_| Enum_ $classLike
}

if (! $classLike->name instanceof Identifier) {
throw new ShouldNotHappenException();
return '';
}

return $classLike->name->toString();
Expand All @@ -409,6 +411,14 @@ private function processTrait(Trait_ $trait, MutatingScope $mutatingScope, calla
{
$traitName = $this->resolveClassName($trait);

if (! $this->reflectionProvider->hasClass($traitName)) {
$trait->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->nodeScopeResolverProcessNodes($trait->stmts, $mutatingScope, $nodeCallback);
$this->decorateTraitAttrGroups($trait, $mutatingScope);

return;
}

$traitClassReflection = $this->reflectionProvider->getClass($traitName);

$traitScope = clone $mutatingScope;
Expand Down

0 comments on commit 16d1df9

Please sign in to comment.