Completed
Pull Request — 2.x (#200)
by Akihito
01:24
created

AnnotatedClassMethods::getNamedKeyVarString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
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 65
     * @var NameKeyVarString
20
     */
21 65
    private $nameKeyVarString;
22 65
23
    public function __construct(Reader $reader)
24 65
    {
25
        $this->reader = $reader;
26 65
        $this->nameKeyVarString = new NameKeyVarString($reader);
27 65
    }
28 62
29
    public function getConstructorName(\ReflectionClass $class) : Name
30 49
    {
31 49
        $constructor = $class->getConstructor();
32
        if (! $constructor) {
33 9
            return new Name(Name::ANY);
34
        }
35 47
        $named = $this->reader->getMethodAnnotation($constructor, Named::class);
36 47
        if ($named instanceof Named) {
37 4
            /* @var $named Named */
38
            return new Name($named->value);
39
        }
40 45
        $name = ($this->nameKeyVarString)($constructor);
41
        if ($name !== null) {
42
            return new Name($name);
43
        }
44
45
        return new Name(Name::ANY);
46 54
    }
47
48 54
    /**
49 54
     * @return null|SetterMethod
50 52
     */
51
    public function getSetterMethod(\ReflectionMethod $method)
52 10
    {
53 10
        $inject = $this->reader->getMethodAnnotation($method, InjectInterface::class);
54 10
        if (! $inject instanceof InjectInterface) {
55 7
            return null;
56
        }
57
        $nameValue = ($this->nameKeyVarString)($method);
58 10
        $setterMethod = new SetterMethod($method, new Name($nameValue));
59
        if ($inject->isOptional()) {
60
            $setterMethod->setOptional();
61
        }
62
63
        return $setterMethod;
64 48
    }
65
}
66