Passed
Push — 2.x ( 6f8018...f6f39e )
by Akihito
02:47 queued 15s
created

NameKeyVarString::__invoke()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
c 0
b 0
f 0
rs 10
cc 4
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\ReflectionClass;
8
use Ray\Aop\ReflectionMethod;
9
use Ray\Di\Di\Named;
10
use Ray\Di\Di\Qualifier;
11
12
use function get_class;
13
use function implode;
14
use function sprintf;
15
16
final class NameKeyVarString
17
{
18
    public function __invoke(ReflectionMethod $method): ?string
19
    {
20
        $keyVal = [];
21
        $named = $method->getAnnotation(Named::class);
22
        if ($named instanceof Named) {
23
            $keyVal[] = $named->value;
24
        }
25
26
        $qualifierNamed = $this->getQualifierKeyVarString($method);
27
        if ($qualifierNamed) {
28
            $keyVal[] = $qualifierNamed;
29
        }
30
31
        return $keyVal !== [] ? implode(',', $keyVal) : null; // var1=qualifier1,va2=qualifier2
32
    }
33
34
    private function getQualifierKeyVarString(ReflectionMethod $method): string
35
    {
36
        $annotations = $method->getAnnotations();
37
        $names = [];
38
        foreach ($annotations as $annotation) {
39
            $qualifier = (new ReflectionClass($annotation))->getAnnotation(Qualifier::class);
40
            if ($qualifier instanceof Qualifier) {
41
                /** @var ?scalar $annotation->value */
42
                $value = $annotation->value ?? Name::ANY; // @phpstan-ignore-line
43
                $names[] = sprintf('%s=%s', (string) $value, get_class($annotation)); // @phpstan-ignore-line
44
            }
45
        }
46
47
        return implode(',', $names);
48
    }
49
}
50