UrlRuleList::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NaokiTsuchiya\YiiRouteList;
6
7
use CUrlManager;
8
use CUrlRule;
9
use NaokiTsuchiya\YiiRouteList\Exception\RuntimeException;
10
use ReflectionClass;
11
use ReflectionProperty;
12
13
final class UrlRuleList
14
{
15
    /** @return list<CUrlRule> */
16
    public function __invoke(CUrlManager $urlManager): array
17
    {
18
        $reflectionClass = new ReflectionClass($urlManager);
19
        $rulesProperty = $this->getRulesProperty($reflectionClass);
20
21
        /** @var list<CUrlRule> $rules */
22
        $rules = $rulesProperty->getValue($urlManager);
23
24
        return $rules;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rules returns the type NaokiTsuchiya\YiiRouteList\list which is incompatible with the type-hinted return array.
Loading history...
25
    }
26
27
    /** @param ReflectionClass<object> $reflectionClass */
28
    private function getRulesProperty(ReflectionClass $reflectionClass): ReflectionProperty
29
    {
30
        if ($reflectionClass->hasProperty('_rules')) {
31
            $_rules = $reflectionClass->getProperty('_rules');
32
            $_rules->setAccessible(true);
33
34
            return $_rules;
35
        }
36
37
        $parent = $reflectionClass->getParentClass();
38
        // @codeCoverageIgnoreStart
39
        if (! $parent) {
40
            throw new RuntimeException("{$reflectionClass->getName()} does not have _rules and parent class");
41
        }
42
43
        // @codeCoverageIgnoreEnd
44
45
        return $this->getRulesProperty($parent);
46
    }
47
}
48