From dbbd52a2ff0829acd12802aab4c0242bfb163537 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 7 Aug 2023 14:10:28 +0200 Subject: [PATCH] Implement ReflectionClass->getInterfaceClassNames() --- src/Reflection/ReflectionClass.php | 6 ++ src/Reflection/ReflectionObject.php | 8 ++ .../Fixture/ClassWithMissingInterface.php | 8 ++ test/unit/Reflection/ReflectionClassTest.php | 88 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 test/unit/Fixture/ClassWithMissingInterface.php diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index c89701aab..179daa6aa 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -1448,6 +1448,12 @@ private function methodHash(string $className, string $methodName): string ); } + /** @return list */ + public function getInterfaceClassNames(): array + { + return $this->implementsClassNames; + } + /** * Gets the interfaces. * diff --git a/src/Reflection/ReflectionObject.php b/src/Reflection/ReflectionObject.php index d2a8e45e7..308d17223 100644 --- a/src/Reflection/ReflectionObject.php +++ b/src/Reflection/ReflectionObject.php @@ -388,6 +388,14 @@ public function getTraitAliases(): array return $this->reflectionClass->getTraitAliases(); } + /** + * {@inheritDoc} + */ + public function getInterfaceClassNames(): array + { + return $this->reflectionClass->getInterfaceClassNames(); + } + /** * {@inheritDoc} */ diff --git a/test/unit/Fixture/ClassWithMissingInterface.php b/test/unit/Fixture/ClassWithMissingInterface.php new file mode 100644 index 000000000..48c81be4d --- /dev/null +++ b/test/unit/Fixture/ClassWithMissingInterface.php @@ -0,0 +1,8 @@ +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 $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}> */ + 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(