diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index 81c6c4ff0..6fad76bb6 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -1112,7 +1112,16 @@ private function addStringableInterface(array $interfaces): array foreach ($this->node->getMethods() as $methodNode) { if ($methodNode->name->toLowerString() === '__tostring') { - $interfaces[$stringableClassName] = $this->reflectClassForNamedNode(new Node\Name(Stringable::class)); + try { + $stringableInterfaceReflection = $this->reflectClassForNamedNode(new Node\Name($stringableClassName)); + + if ($stringableInterfaceReflection->isInternal()) { + $interfaces[$stringableClassName] = $stringableInterfaceReflection; + } + } catch (IdentifierNotFound) { + // Stringable interface does not exist on target PHP version + } + break; } } diff --git a/test/unit/Reflection/ReflectionClassTest.php b/test/unit/Reflection/ReflectionClassTest.php index 9c1409f2a..a2719cce4 100644 --- a/test/unit/Reflection/ReflectionClassTest.php +++ b/test/unit/Reflection/ReflectionClassTest.php @@ -2383,6 +2383,50 @@ public function __toString(); self::assertArrayHasKey(Stringable::class, $interfaceNotExtendingStringable->getImmediateInterfaces()); } + public function testNoStringableInterfaceWhenStringableNotFound(): void + { + $php = <<<'PHP' + astLocator)); + + $noStringable = $reflector->reflectClass('NoStringable'); + + self::assertNotContains(Stringable::class, $noStringable->getInterfaceNames()); + } + + public function testNoStringableInterfaceWhenStringableIsNotInternal(): void + { + $php = <<<'PHP' + astLocator)); + + $noStringable = $reflector->reflectClass('NoStringable'); + + self::assertNotContains(Stringable::class, $noStringable->getInterfaceNames()); + } + public function testHasAllInterfacesWithStringable(): void { $php = <<<'PHP'