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

AnnotatedClassMethods::getNamedKeyVarString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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\InjectInterface;
9
use Ray\Di\Di\Named;
10
11
final class AnnotatedClassMethods
12
{
13
    /**
14
     * @var Reader
15
     */
16
    private $reader;
17
18
    /**
19
     * @var NameKeyVarString
20
     */
21
    private $nameKeyVarString;
22
23 65
    public function __construct(Reader $reader)
24
    {
25 65
        $this->reader = $reader;
26 65
        $this->nameKeyVarString = new NameKeyVarString($reader);
27
    }
28 65
29
    /**
30 65
     * @phpstan-param \ReflectionClass<object> $class
31 65
     */
32 62
    public function getConstructorName(\ReflectionClass $class) : Name
33
    {
34 49
        $constructor = $class->getConstructor();
35 49
        if (! $constructor) {
36
            return new Name(Name::ANY);
37 9
        }
38
        $named = $this->reader->getMethodAnnotation($constructor, Named::class);
39 47
        if ($named instanceof Named) {
40 47
            /* @var $named Named */
41 4
            return new Name($named->value);
42
        }
43
        $name = ($this->nameKeyVarString)($constructor);
44 45
        if ($name !== null) {
45
            return new Name($name);
46
        }
47
48
        return new Name(Name::ANY);
49
    }
50
51
    /**
52 54
     * @return ?SetterMethod
0 ignored issues
show
Documentation introduced by
The doc-type ?SetterMethod could not be parsed: Unknown type name "?SetterMethod" 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...
53
     */
54 54
    public function getSetterMethod(\ReflectionMethod $method)
55 54
    {
56 52
        $inject = $this->reader->getMethodAnnotation($method, InjectInterface::class);
57
        if (! $inject instanceof InjectInterface) {
58 10
            return null;
59 10
        }
60 10
        $nameValue = ($this->nameKeyVarString)($method);
61 7
        $setterMethod = new SetterMethod($method, new Name($nameValue));
62
        if ($inject->isOptional()) {
63
            $setterMethod->setOptional();
64 10
        }
65
66
        return $setterMethod;
67
    }
68
}
69