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

Implement ReflectionClass#getInterfaceClassNames() #1361

Merged
merged 1 commit into from
Aug 7, 2023
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
6 changes: 6 additions & 0 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,12 @@ private function methodHash(string $className, string $methodName): string
);
}

/** @return list<class-string> */
public function getInterfaceClassNames(): array
{
return $this->implementsClassNames;
}

/**
* Gets the interfaces.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Reflection/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ public function getTraitAliases(): array
return $this->reflectionClass->getTraitAliases();
}

/**
* {@inheritDoc}
*/
public function getInterfaceClassNames(): array
{
return $this->reflectionClass->getInterfaceClassNames();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions test/unit/Fixture/ClassWithMissingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ClassWithMissingInterface implements InterfaceThatDoesNotExist
{

}
88 changes: 88 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
use Roave\BetterReflectionTest\Fixture\ClassUsingTraitWithAbstractMethod;
use Roave\BetterReflectionTest\Fixture\ClassWithAttributes;
use Roave\BetterReflectionTest\Fixture\ClassWithCaseInsensitiveMethods;
use Roave\BetterReflectionTest\Fixture\ClassWithMissingInterface;
use Roave\BetterReflectionTest\Fixture\ClassWithMissingParent;
use Roave\BetterReflectionTest\Fixture\ClassWithNonAbstractTraitMethodThatOverwritePreviousAbstractTraitMethod;
use Roave\BetterReflectionTest\Fixture\DefaultProperties;
Expand Down Expand Up @@ -425,6 +426,93 @@ public function testGetParentClassWithMissingParent(): void
$classInfo->getParentClass();
}

public function testGetInterfaceNamesWithMissingInterfaceDefinitions(): void
{
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassWithMissingInterface.php',
$this->astLocator,
)))->reflectClass(ClassWithMissingInterface::class);

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

self::assertNotNull($classInfo->getInterfaceNames());
}

public function testGetInterfacesWithMissingInterfaceDefinitions(): void
{
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassWithMissingInterface.php',
$this->astLocator,
)))->reflectClass(ClassWithMissingInterface::class);

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

self::assertNotNull($classInfo->getInterfaces());
}
Comment on lines +429 to +451
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests show, that the pre-PR existing API always throws and therefore does not allow to get the interface names, when the class-symbols are not autoloadable.


/** @param list<class-string> $expectedInterfaces */
#[DataProvider('getInterfaceClassNamesDataProvider')]
public function testGetInterfaceClassNames(string $sourcePath, string $className, array $expectedInterfaces): void
{
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
$sourcePath,
$this->astLocator,
)))->reflectClass($className);

self::assertSame(
$expectedInterfaces,
$classInfo->getInterfaceClassNames(),
);
}

/** @return list<array{string, class-string, list<class-string>}> */
public static function getInterfaceClassNamesDataProvider(): array
{
return [
[
__DIR__ . '/../Fixture/ClassWithMissingInterface.php',
ClassWithMissingInterface::class,
['Roave\BetterReflectionTest\Fixture\InterfaceThatDoesNotExist'],
],
Comment on lines +472 to +476
Copy link
Contributor Author

@staabm staabm Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this first test-case shows that we are now able to extract the interface names even if not autoloadable.

the remaining cases add coverage for a few more related cases like autoloadable classes, traits,..

[
__DIR__ . '/../Fixture/ClassWithInterfaces.php',
ClassWithInterfaces\ExampleClass::class,
[
'Roave\BetterReflectionTest\ClassWithInterfaces\A',
'Roave\BetterReflectionTest\ClassWithInterfacesOther\B',
'Roave\BetterReflectionTest\ClassWithInterfaces\C',
'Roave\BetterReflectionTest\ClassWithInterfacesOther\D',
'E',
],
],
[
__DIR__ . '/../Fixture/ClassWithInterfaces.php',
ClassWithInterfaces\SubExampleClass::class,
[],
],
[
__DIR__ . '/../Fixture/ClassWithInterfaces.php',
ClassWithInterfaces\ExampleImplementingCompositeInterface::class,
['Roave\BetterReflectionTest\ClassWithInterfacesExtendingInterfaces\D'],
],
[
__DIR__ . '/../Fixture/EmptyTrait.php',
Fixture\EmptyTrait::class,
[],
],
[
__DIR__ . '/../Fixture/Enums.php',
IntEnum::class,
['Roave\BetterReflectionTest\Fixture\InterfaceForEnum'],
],
[
__DIR__ . '/../Fixture/Enums.php',
Fixture\IsDeprecated::class,
[],
],
];
}

public function testGetMethodsOrder(): void
{
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
Expand Down