Skip to content

Commit

Permalink
Implement ReflectionClass->getInterfaceClassNames()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Aug 7, 2023
1 parent 37becb2 commit dbbd52a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
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());
}

/** @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'],
],
[
__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

0 comments on commit dbbd52a

Please sign in to comment.