diff --git a/src/Analyser/ConstantResolver.php b/src/Analyser/ConstantResolver.php index d300bb0379..9dc79e20d4 100644 --- a/src/Analyser/ConstantResolver.php +++ b/src/Analyser/ConstantResolver.php @@ -14,10 +14,12 @@ use PHPStan\Type\GeneralizePrecision; use PHPStan\Type\IntegerRangeType; use PHPStan\Type\IntersectionType; +use PHPStan\Type\MixedType; use PHPStan\Type\ResourceType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; +use function array_key_exists; use function in_array; use const INF; use const NAN; @@ -26,6 +28,9 @@ class ConstantResolver { + /** @var array */ + private array $currentlyResolving = []; + /** * @param string[] $dynamicConstantNames */ @@ -47,10 +52,19 @@ public function resolveConstant(Name $name, ?NamespaceAnswerer $scope): ?Type return $constantType; } + if (array_key_exists($resolvedConstantName, $this->currentlyResolving)) { + return new MixedType(); + } + + $this->currentlyResolving[$resolvedConstantName] = true; + $constantReflection = $this->getReflectionProvider()->getConstant($name, $scope); $constantType = $constantReflection->getValueType(); - return $this->resolveConstantType($resolvedConstantName, $constantType); + $type = $this->resolveConstantType($resolvedConstantName, $constantType); + unset($this->currentlyResolving[$resolvedConstantName]); + + return $type; } public function resolvePredefinedConstant(string $resolvedConstantName): ?Type