diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index be05271cf1..f4603d1fee 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3231,12 +3231,17 @@ public function enterPropertyHook( private function transformStaticType(Type $type): Type { + if (!$this->isInClass()) { + return $type; + } + return TypeTraverser::map($type, function (Type $type, callable $traverse): Type { - if (!$this->isInClass()) { - return $type; - } if ($type instanceof StaticType) { $classReflection = $this->getClassReflection(); + if ($classReflection === null) { + throw new ShouldNotHappenException(); + } + $changedType = $type->changeBaseClass($classReflection); if ($classReflection->isFinal() && !$type instanceof ThisType) { $changedType = $changedType->getStaticObjectType(); diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index c76661d6d2..0729ed5e0a 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -2400,9 +2400,11 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con } if (strtolower($constantName) === 'class') { + $reflectionProvider = $this->getReflectionProvider(); + return TypeTraverser::map( $constantClassType, - function (Type $type, callable $traverse): Type { + static function (Type $type, callable $traverse) use ($reflectionProvider): Type { if ($type instanceof UnionType || $type instanceof IntersectionType) { return $traverse($type); } @@ -2428,8 +2430,8 @@ function (Type $type, callable $traverse): Type { new GenericClassStringType($type), new AccessoryLiteralStringType(), ); - } elseif ($objectClassNames !== [] && $this->getReflectionProvider()->hasClass($objectClassNames[0])) { - $reflection = $this->getReflectionProvider()->getClass($objectClassNames[0]); + } elseif ($objectClassNames !== [] && $reflectionProvider->hasClass($objectClassNames[0])) { + $reflection = $reflectionProvider->getClass($objectClassNames[0]); if ($reflection->isFinalByKeyword()) { return new ConstantStringType($reflection->getName(), true); }