Total Complexity | 10 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Coverage | 93.55% |
Changes | 0 |
1 | <?php |
||
15 | trait DocBlockResolverAwareTrait |
||
16 | { |
||
17 | /** @var array<string,string> */ |
||
18 | protected static array $fileContentCache = []; |
||
19 | |||
20 | /** @var array<string,?object> */ |
||
21 | private array $customServices = []; |
||
22 | |||
23 | 2 | public function __call(string $method, array $arguments = []): ?object |
|
24 | { |
||
25 | 2 | if (!isset($this->customServices[$method])) { |
|
26 | 2 | $className = $this->getClassFromDoc($method); |
|
27 | 2 | $resolvableType = $this->normalizeResolvableType($className); |
|
28 | |||
29 | 2 | $this->customServices[$method] = (new DocBlockServiceResolver($resolvableType)) |
|
30 | 2 | ->resolve($className); |
|
31 | } |
||
32 | |||
33 | 2 | return $this->customServices[$method]; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return class-string |
||
1 ignored issue
–
show
|
|||
38 | */ |
||
39 | 2 | private function getClassFromDoc(string $method): string |
|
40 | { |
||
41 | 2 | $reflectionClass = new ReflectionClass(static::class); |
|
42 | 2 | $className = $this->searchClassOverDocBlock($reflectionClass, $method); |
|
43 | 2 | if (class_exists($className)) { |
|
44 | return $className; |
||
45 | } |
||
46 | |||
47 | 2 | $className = $this->searchClassOverUseStatements($reflectionClass, $className); |
|
48 | 2 | if (class_exists($className)) { |
|
49 | 2 | return $className; |
|
50 | } |
||
51 | |||
52 | throw MissingClassDefinitionException::missingDefinition(static::class, $method, $className); |
||
53 | } |
||
54 | |||
55 | 2 | private function searchClassOverDocBlock(ReflectionClass $reflectionClass, string $method): string |
|
56 | { |
||
57 | 2 | $docBlock = (string)$reflectionClass->getDocComment(); |
|
58 | |||
59 | 2 | return (new DocBlockParser())->getClassFromMethod($docBlock, $method); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Look the uses, to find the fully-qualified class name for the className. |
||
64 | */ |
||
65 | 2 | private function searchClassOverUseStatements(ReflectionClass $reflectionClass, string $className): string |
|
74 | } |
||
75 | |||
76 | 2 | private function normalizeResolvableType(string $resolvableType): string |
|
77 | { |
||
87 |