diff --git a/src/Functions.php b/src/Functions.php index 9f3e746..00ffef5 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -159,7 +159,7 @@ function class_uses_recursive($class) $results = []; /* @phpstan-ignore-next-line */ - foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) { + foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) { $results += trait_uses_recursive($class); } diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 48f4aae..a11784a 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -11,11 +11,16 @@ */ namespace HyperfTest\Support; +use HyperfTest\Support\Stub\Bar; +use HyperfTest\Support\Stub\Foo; +use HyperfTest\Support\Stub\Traits\BarTrait; +use HyperfTest\Support\Stub\Traits\FooTrait; use HyperfTest\Utils\Exception\RetryException; use HyperfTest\Utils\Stub\FooClosure; use PHPUnit\Framework\TestCase; use function Hyperf\Support\call; +use function Hyperf\Support\class_uses_recursive; use function Hyperf\Support\env; use function Hyperf\Support\retry; use function Hyperf\Support\swoole_hook_flags; @@ -124,4 +129,21 @@ public function testEnv() $this->assertNull(env($id)); } + + public function testClassUsesRecursive() + { + $this->assertSame( + [ + FooTrait::class => FooTrait::class, + ], + class_uses_recursive(Foo::class) + ); + $this->assertSame( + [ + FooTrait::class => FooTrait::class, + BarTrait::class => BarTrait::class, + ], + class_uses_recursive(Bar::class) + ); + } } diff --git a/tests/Stub/Bar.php b/tests/Stub/Bar.php new file mode 100644 index 0000000..cdf5206 --- /dev/null +++ b/tests/Stub/Bar.php @@ -0,0 +1,19 @@ +