Completed
Push — master ( 8ecded...ff0374 )
by Matthieu
11s
created

AnnotationBasedAutowiring::readProperties()   B

↳ Parent: AnnotationBasedAutowiring

Complexity

Conditions 6
Paths 12

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 20
Code Lines 10

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 12
nop 2
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace DI\Definition\Source;
4
5
use DI\Annotation\Inject;
6
use DI\Annotation\Injectable;
7
use DI\Definition\AutowireDefinition;
8
use DI\Definition\EntryReference;
9
use DI\Definition\Exception\AnnotationException;
10
use DI\Definition\ObjectDefinition;
11
use DI\Definition\ObjectDefinition\MethodInjection;
12
use DI\Definition\ObjectDefinition\PropertyInjection;
13
use Doctrine\Common\Annotations\AnnotationRegistry;
14
use Doctrine\Common\Annotations\Reader;
15
use Doctrine\Common\Annotations\SimpleAnnotationReader;
16
use InvalidArgumentException;
17
use PhpDocReader\PhpDocReader;
18
use ReflectionClass;
19
use ReflectionMethod;
20
use ReflectionParameter;
21
use ReflectionProperty;
22
use UnexpectedValueException;
23
24
/**
25
 * Provides DI definitions by reading annotations such as @ Inject and @ var annotations.
26
 *
27
 * Uses Autowiring, Doctrine's Annotations and regex docblock parsing.
28
 * This source automatically includes the reflection source.
29
 *
30
 * @author Matthieu Napoli <[email protected]>
31
 */
32
class AnnotationBasedAutowiring implements DefinitionSource, Autowiring
33
{
34
    /**
35
     * @var Reader
36
     */
37
    private $annotationReader;
38
39
    /**
40
     * @var PhpDocReader
41
     */
42
    private $phpDocReader;
43
44
    /**
45
     * @var bool
46
     */
47
    private $ignorePhpDocErrors;
48
49
    public function __construct($ignorePhpDocErrors = false)
50
    {
51
        $this->ignorePhpDocErrors = (bool) $ignorePhpDocErrors;
52
    }
53
54
    public function autowire(string $name, AutowireDefinition $definition = null)
55
    {
56
        $className = $definition ? $definition->getClassName() : $name;
57
58
        if (!class_exists($className) && !interface_exists($className)) {
59
            return $definition;
60
        }
61
62
        $definition = $definition ?: new AutowireDefinition($name);
63
64
        $class = new ReflectionClass($className);
65
66
        $this->readInjectableAnnotation($class, $definition);
67
68
        // Browse the class properties looking for annotated properties
69
        $this->readProperties($class, $definition);
70
71
        // Browse the object's methods looking for annotated methods
72
        $this->readMethods($class, $definition);
73
74
        return $definition;
75
    }
76
77
    /**
78
     * [email protected]}
79
     * @throws AnnotationException
80
     * @throws InvalidArgumentException The class doesn't exist
81
     */
82
    public function getDefinition($name)
83
    {
84
        return $this->autowire($name);
85
    }
86
87
    /**
88
     * Browse the class properties looking for annotated properties.
89
     */
90
    private function readProperties(ReflectionClass $class, ObjectDefinition $definition)
91
    {
92
        foreach ($class->getProperties() as $property) {
93
            if ($property->isStatic()) {
94
                continue;
95
            }
96
            $this->readProperty($property, $definition);
97
        }
98
99
        // Read also the *private* properties of the parent classes
100
        /** @noinspection PhpAssignmentInConditionInspection */
101
        while ($class = $class->getParentClass()) {
102
            foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE) as $property) {
103
                if ($property->isStatic()) {
104
                    continue;
105
                }
106
                $this->readProperty($property, $definition, $class->getName());
107
            }
108
        }
109
    }
110
111
    private function readProperty(ReflectionProperty $property, ObjectDefinition $definition, $classname = null)
112
    {
113
        // Look for @Inject annotation
114
        /** @var $annotation Inject */
115
        $annotation = $this->getAnnotationReader()->getPropertyAnnotation($property, 'DI\Annotation\Inject');
116
        if ($annotation === null) {
117
            return null;
118
        }
119
120
        // @Inject("name") or look for @var content
121
        $entryName = $annotation->getName() ?: $this->getPhpDocReader()->getPropertyClass($property);
122
123
        if ($entryName === null) {
124
            throw new AnnotationException(sprintf(
125
                '@Inject found on property %s::%s but unable to guess what to inject, use a @var annotation',
126
                $property->getDeclaringClass()->getName(),
127
                $property->getName()
128
            ));
129
        }
130
131
        $definition->addPropertyInjection(
132
            new PropertyInjection($property->getName(), new EntryReference($entryName), $classname)
133
        );
134
    }
135
136
    /**
137
     * Browse the object's methods looking for annotated methods.
138
     */
139
    private function readMethods(ReflectionClass $class, ObjectDefinition $objectDefinition)
140
    {
141
        // This will look in all the methods, including those of the parent classes
142
        foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
143
            if ($method->isStatic()) {
144
                continue;
145
            }
146
147
            $methodInjection = $this->getMethodInjection($method);
0 ignored issues
show
Bug introduced by Matthieu Napoli
Are you sure the assignment to $methodInjection is correct as $this->getMethodInjection($method) (which targets DI\Definition\Source\Ann...g::getMethodInjection()) 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...
148
149
            if (! $methodInjection) {
150
                continue;
151
            }
152
153
            if ($method->isConstructor()) {
154
                $objectDefinition->setConstructorInjection($methodInjection);
155
            } else {
156
                $objectDefinition->addMethodInjection($methodInjection);
157
            }
158
        }
159
    }
160
161
    private function getMethodInjection(ReflectionMethod $method)
162
    {
163
        // Look for @Inject annotation
164
        /** @var $annotation Inject|null */
165
        try {
166
            $annotation = $this->getAnnotationReader()->getMethodAnnotation($method, 'DI\Annotation\Inject');
167
        } catch (AnnotationException $e) {
168
            throw new AnnotationException(sprintf(
169
                '@Inject annotation on %s::%s is malformed. %s',
170
                $method->getDeclaringClass()->getName(),
171
                $method->getName(),
172
                $e->getMessage()
173
            ), 0, $e);
174
        }
175
        $annotationParameters = $annotation ? $annotation->getParameters() : [];
176
177
        // @Inject on constructor is implicit
178
        if (! ($annotation || $method->isConstructor())) {
179
            return null;
180
        }
181
182
        $parameters = [];
183
        foreach ($method->getParameters() as $index => $parameter) {
184
            $entryName = $this->getMethodParameter($index, $parameter, $annotationParameters);
185
186
            if ($entryName !== null) {
187
                $parameters[$index] = new EntryReference($entryName);
188
            }
189
        }
190
191
        if ($method->isConstructor()) {
192
            return MethodInjection::constructor($parameters);
193
        } else {
194
            return new MethodInjection($method->getName(), $parameters);
195
        }
196
    }
197
198
    /**
199
     * @param int                 $parameterIndex
200
     * @param ReflectionParameter $parameter
201
     * @param array               $annotationParameters
202
     *
203
     * @return string|null Entry name or null if not found.
204
     */
205
    private function getMethodParameter($parameterIndex, ReflectionParameter $parameter, array $annotationParameters)
206
    {
207
        // @Inject has definition for this parameter (by index, or by name)
208
        if (isset($annotationParameters[$parameterIndex])) {
209
            return $annotationParameters[$parameterIndex];
210
        }
211
        if (isset($annotationParameters[$parameter->getName()])) {
212
            return $annotationParameters[$parameter->getName()];
213
        }
214
215
        // Skip optional parameters if not explicitly defined
216
        if ($parameter->isOptional()) {
217
            return null;
218
        }
219
220
        // Try to use the type-hinting
221
        $parameterClass = $parameter->getClass();
222
        if ($parameterClass) {
223
            return $parameterClass->getName();
224
        }
225
226
        // Last resort, look for @param tag
227
        return $this->getPhpDocReader()->getParameterClass($parameter);
228
    }
229
230
    /**
231
     * @return Reader The annotation reader
232
     */
233
    public function getAnnotationReader()
234
    {
235
        if ($this->annotationReader === null) {
236
            AnnotationRegistry::registerAutoloadNamespace('DI\Annotation', __DIR__ . '/../../../');
237
            $this->annotationReader = new SimpleAnnotationReader();
238
            $this->annotationReader->addNamespace('DI\Annotation');
239
        }
240
241
        return $this->annotationReader;
242
    }
243
244
    /**
245
     * @return PhpDocReader
246
     */
247
    private function getPhpDocReader()
248
    {
249
        if ($this->phpDocReader === null) {
250
            $this->phpDocReader = new PhpDocReader($this->ignorePhpDocErrors);
251
        }
252
253
        return $this->phpDocReader;
254
    }
255
256
    private function readInjectableAnnotation(ReflectionClass $class, ObjectDefinition $definition)
257
    {
258
        try {
259
            /** @var $annotation Injectable|null */
260
            $annotation = $this->getAnnotationReader()
261
                ->getClassAnnotation($class, 'DI\Annotation\Injectable');
262
        } catch (UnexpectedValueException $e) {
263
            throw new AnnotationException(sprintf(
264
                'Error while reading @Injectable on %s: %s',
265
                $class->getName(),
266
                $e->getMessage()
267
            ), 0, $e);
268
        }
269
270
        if (! $annotation) {
271
            return;
272
        }
273
274
        if ($annotation->getScope()) {
0 ignored issues
show
Bug Best Practice introduced by Matthieu Napoli
The expression $annotation->getScope() 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...
275
            $definition->setScope($annotation->getScope());
276
        }
277
        if ($annotation->isLazy() !== null) {
278
            $definition->setLazy($annotation->isLazy());
279
        }
280
    }
281
}
282