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

AnnotatedClassMethods::getNamedKeyVarString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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