Skip to content

Commit

Permalink
Handle class_parents returning false in class_uses_recursive (#…
Browse files Browse the repository at this point in the history
…6261)
  • Loading branch information
huangdijia authored Nov 6, 2023
1 parent 71eda50 commit e3690f0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
}
19 changes: 19 additions & 0 deletions tests/Stub/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Support\Stub;

use HyperfTest\Support\Stub\Traits\BarTrait;

class Bar extends Foo
{
use BarTrait;
}
19 changes: 19 additions & 0 deletions tests/Stub/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Support\Stub;

use HyperfTest\Support\Stub\Traits\FooTrait;

class Foo
{
use FooTrait;
}
16 changes: 16 additions & 0 deletions tests/Stub/Traits/BarTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Support\Stub\Traits;

trait BarTrait
{
}
16 changes: 16 additions & 0 deletions tests/Stub/Traits/FooTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Support\Stub\Traits;

trait FooTrait
{
}

0 comments on commit e3690f0

Please sign in to comment.