Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detection of interface extends circular references #1241

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,23 +1600,32 @@ private function getCurrentClassImplementedInterfacesIndexedByName(): array
/**
* This method allows us to retrieve all interfaces parent of the this interface. Do not use on class nodes!
*
* @param list<class-string> $interfaceClassNames
*
* @return array<class-string, ReflectionClass> parent interfaces of this interface
*
* @throws NotAnInterfaceReflection
*/
private function getInterfacesHierarchy(): array
private function getInterfacesHierarchy(array &$interfaceClassNames = []): array
{
if (! $this->isInterface) {
throw NotAnInterfaceReflection::fromReflectionClass($this);
}

$interfaceClassName = $this->getName();
if (in_array($interfaceClassName, $interfaceClassNames, true)) {
throw CircularReference::fromClassName($interfaceClassName);
}

$interfaceClassNames[] = $interfaceClassName;

/** @var array<class-string, self> $interfaces */
$interfaces = array_merge(
[$this->getName() => $this],
[$interfaceClassName => $this],
...array_map(
fn (string $interfaceClassName): array => $this->reflector
->reflectClass($interfaceClassName)
->getInterfacesHierarchy(),
->getInterfacesHierarchy($interfaceClassNames),
$this->implementsClassNames,
),
);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/Fixture/InvalidInterfaceParents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Roave\BetterReflectionTest\Fixture\InvalidInterfaceParents;

interface InterfaceExtendsSelf extends InterfaceExtendsSelf {}

interface Interface1 extends Interface2 {}
interface Interface2 extends Interface1 {}
interface Interface3 extends Interface2 {}
26 changes: 26 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,32 @@ public function testGetInterfacesWillConsiderInterfaceInheritanceLevels(): void
}
}

/** @return list<array{0: string}> */
public function interfaceExtendsCircularReferencesProvider(): array
{
return [
['Roave\\BetterReflectionTest\\Fixture\\InvalidInterfaceParents\\InterfaceExtendsSelf'],
['Roave\\BetterReflectionTest\\Fixture\\InvalidInterfaceParents\\Interface1'],
['Roave\\BetterReflectionTest\\Fixture\\InvalidInterfaceParents\\Interface2'],
['Roave\\BetterReflectionTest\\Fixture\\InvalidInterfaceParents\\Interface3'],
];
}

/** @dataProvider interfaceExtendsCircularReferencesProvider */
public function testGetInterfacesFailsWithCircularReferences(string $className): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/InvalidInterfaceParents.php',
$this->astLocator,
));

$class = $reflector->reflectClass($className);

$this->expectException(CircularReference::class);

$class->getInterfaces();
}

public function testIsInstance(): void
{
// note: ClassForHinting is safe to type-check against, as it will actually be loaded at runtime
Expand Down