Completed
Branch 2.x (dc1b30)
by Akihito
02:25
created

AnnotatedClassMethods::getSetterMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 16
cc 3
eloc 9
c 3
b 0
f 0
nc 3
nop 1
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keyVal of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $qualifier is correct as $this->reader->getClassA...ay\\Di\\Di\\Qualifier') (which targets Doctrine\Common\Annotati...r::getClassAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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