Completed
Push — 2.x ( e0ffa3...2bc12f )
by Akihito
03:04
created

NameKeyVarString::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
        /** @var array<object> $annotations */
41
        $annotations = $this->reader->getMethodAnnotations($method);
42
        $names = [];
43
        foreach ($annotations as $annotation) {
44
            $qualifier = $this->reader->getClassAnnotation(new \ReflectionClass($annotation), Qualifier::class);
45
            if ($qualifier instanceof Qualifier) {
46
                /** @var ?scalar $annotation->value */
0 ignored issues
show
Documentation introduced by
The doc-type ?scalar could not be parsed: Unknown type name "?scalar" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
47
                $value = $annotation->value ?? Name::ANY; // @phpstan-ignore-line
48
                $names[] = sprintf('%s=%s', (string) $value, \get_class($annotation)); // @phpstan-ignore-line
49
            }
50
        }
51
52
        return implode(',', $names);
53
    }
54
}
55