sunrise-php /
http-router
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * It's free open-source software released under the MIT License. |
||
| 5 | * |
||
| 6 | * @author Anatoly Nekhay <[email protected]> |
||
| 7 | * @copyright Copyright (c) 2018, Anatoly Nekhay |
||
| 8 | * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
||
| 9 | * @link https://github.com/sunrise-php/http-router |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace Sunrise\Http\Router\Helper; |
||
| 15 | |||
| 16 | use ReflectionClass; |
||
| 17 | use ReflectionMethod; |
||
| 18 | |||
| 19 | use function array_reverse; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @since 3.0.0 |
||
| 23 | */ |
||
| 24 | final class ReflectorHelper |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @param ReflectionClass<object> $class |
||
| 28 | * |
||
| 29 | * @return array<array-key, ReflectionClass<object>> |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 30 | */ |
||
| 31 | 49 | public static function getClassAncestry(ReflectionClass $class): array |
|
| 32 | { |
||
| 33 | 49 | $ancestry = [$class]; |
|
| 34 | 49 | while ($class = $class->getParentClass()) { |
|
| 35 | 8 | $ancestry[] = $class; |
|
| 36 | } |
||
| 37 | |||
| 38 | 49 | return array_reverse($ancestry); |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param ReflectionMethod $method |
||
| 43 | * |
||
| 44 | * @return array<array-key, ReflectionClass<object>|ReflectionMethod> |
||
|
0 ignored issues
–
show
|
|||
| 45 | */ |
||
| 46 | 47 | public static function getMethodAncestry(ReflectionMethod $method): array |
|
| 47 | { |
||
| 48 | 47 | $ancestry = self::getClassAncestry($method->getDeclaringClass()); |
|
| 49 | 47 | $ancestry[] = $method; |
|
| 50 | |||
| 51 | 47 | return $ancestry; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param ReflectionClass<object>|ReflectionMethod $proband |
||
| 56 | * |
||
| 57 | * @return array<array-key, ReflectionClass<object>|ReflectionMethod> |
||
|
0 ignored issues
–
show
|
|||
| 58 | */ |
||
| 59 | 49 | public static function getAncestry(ReflectionClass|ReflectionMethod $proband): array |
|
| 60 | { |
||
| 61 | 49 | return $proband instanceof ReflectionClass |
|
| 62 | 6 | ? self::getClassAncestry($proband) |
|
| 63 | 49 | : self::getMethodAncestry($proband); |
|
| 64 | } |
||
| 65 | } |
||
| 66 |