Completed
Push — 2.x ( 911306...bcb56c )
by Akihito
8s
created

src/AnnotatedClassMethods.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Doctrine\Common\Annotations\Reader;
10
use Ray\Di\Di\Named;
11
12
final class AnnotatedClassMethods
13
{
14
    /**
15
     * @var Reader
16
     */
17
    private $reader;
18
19 44
    public function __construct(Reader $reader)
20
    {
21 44
        $this->reader = $reader;
22 44
    }
23
24
    /**
25
     * @param \ReflectionClass $class
26
     *
27
     * @return Name
28
     */
29 44
    public function getConstructorName(\ReflectionClass $class)
30
    {
31 44
        $constructor = $class->getConstructor();
32 44
        if (! $constructor) {
33 36
            return new Name(Name::ANY);
34
        }
35 17
        $named = $this->reader->getMethodAnnotation($constructor, 'Ray\Di\Di\Named');
36 17
        if ($named) {
37
            /* @var $named Named */
38 8
            return new Name($named->value);
39
        }
40 15
        $name = $this->getNamedKeyVarString($constructor);
41 15
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42 4
            return new Name($name);
43
        }
44
45 12
        return new Name(Name::ANY);
46
    }
47
48
    /**
49
     * @param \ReflectionMethod $method
50
     *
51
     * @return SetterMethod
52
     */
53 25
    public function getSetterMethod(\ReflectionMethod $method)
54
    {
55 25
        $inject = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\InjectInterface');
56
57
        /* @var $inject \Ray\Di\Di\Inject */
58 25
        if (! $inject) {
59 22
            return null;
60
        }
61 10
        $nameValue = $this->getNamedKeyVarString($method);
62 10
        $setterMethod = new SetterMethod($method, new Name($nameValue));
63 10
        if ($inject->isOptional()) {
64 7
            $setterMethod->setOptional();
65 7
        }
66
67 10
        return $setterMethod;
68
    }
69
70
    /**
71
     * @param \ReflectionMethod $method
72
     *
73
     * @return string
74
     */
75 16
    private function getNamedKeyVarString(\ReflectionMethod $method)
76
    {
77 16
        $keyVal = [];
78
        /* @var $named Named */
79 16
        $named = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\Named');
80 16
        if ($named) {
81 7
            $keyVal[] = $named->value;
82 7
        }
83 16
        $qualifierNamed = $this->getQualifierKeyVarString($method);
84 16
        if ($qualifierNamed) {
85 12
            $keyVal[] = $qualifierNamed;
86 12
        }
87 16
        if ($keyVal) {
88 12
            return implode(',', $keyVal); // var1=qualifier1,va2=qualifier2
89
        }
90
91 12
        return null;
92
    }
93
94
    /**
95
     * @param \ReflectionMethod $method
96
     *
97
     * @return string
98
     */
99 16
    private function getQualifierKeyVarString(\ReflectionMethod $method)
100
    {
101 16
        $annotations = $this->reader->getMethodAnnotations($method);
102 16
        $names = [];
103 16
        foreach ($annotations as $annotation) {
104
            /* @var $bindAnnotation object|null */
105 12
            $qualifier = $this->reader->getClassAnnotation(new \ReflectionClass($annotation), 'Ray\Di\Di\Qualifier');
106 12
            if ($qualifier) {
107 12
                $value = isset($annotation->value) ? $annotation->value : Name::ANY;
108 12
                $names[] = sprintf('%s=%s', $value, get_class($annotation));
109 12
            }
110 16
        }
111
112 16
        return implode(',', $names);
113
    }
114
}
115