ReflectorHelper::getMethodAncestry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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
The doc comment array<array-key, ReflectionClass<object>> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, ReflectionClass<object>>.
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
Documentation Bug introduced by
The doc comment array<array-key, Reflect...ject>|ReflectionMethod> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, ReflectionClass<object>|ReflectionMethod>.
Loading history...
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
Documentation Bug introduced by
The doc comment array<array-key, Reflect...ject>|ReflectionMethod> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, ReflectionClass<object>|ReflectionMethod>.
Loading history...
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