Completed
Push — metrics-refactor ( 233b6e )
by Akihito
01:31
created

NameKeyVarString::__invoke()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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