|
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\AnnotationReader; |
|
10
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
11
|
|
|
|
|
12
|
|
|
final class AnnotatedClass |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var AnnotationReader |
|
16
|
|
|
*/ |
|
17
|
|
|
private $reader; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var AnnotatedClassMethods |
|
21
|
|
|
*/ |
|
22
|
|
|
private $injectionMethod; |
|
23
|
|
|
|
|
24
|
44 |
|
public function __construct(AnnotationReader $reader) |
|
25
|
|
|
{ |
|
26
|
44 |
|
AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php'); |
|
27
|
44 |
|
$this->reader = $reader; |
|
28
|
44 |
|
$this->injectionMethod = new AnnotatedClassMethods($reader); |
|
29
|
44 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return factory instance |
|
33
|
|
|
* |
|
34
|
|
|
* @param \ReflectionClass $class Target class reflection |
|
35
|
|
|
* |
|
36
|
|
|
* @return NewInstance |
|
37
|
|
|
*/ |
|
38
|
44 |
|
public function getNewInstance(\ReflectionClass $class) |
|
39
|
|
|
{ |
|
40
|
44 |
|
$setterMethods = new SetterMethods([]); |
|
41
|
44 |
|
$methods = $class->getMethods(); |
|
42
|
44 |
|
foreach ($methods as $method) { |
|
43
|
30 |
|
if ($method->name === '__construct') { |
|
44
|
17 |
|
continue; |
|
45
|
|
|
} |
|
46
|
25 |
|
$setterMethods->add($this->injectionMethod->getSetterMethod($method)); |
|
47
|
44 |
|
} |
|
48
|
44 |
|
$name = $this->injectionMethod->getConstructorName($class); |
|
49
|
44 |
|
$newInstance = new NewInstance($class, $setterMethods, $name); |
|
50
|
|
|
|
|
51
|
44 |
|
return $newInstance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return @-PostConstruct method reflection |
|
56
|
|
|
* |
|
57
|
|
|
* @param \ReflectionClass $class |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|\ReflectionMethod |
|
60
|
|
|
*/ |
|
61
|
44 |
|
public function getPostConstruct(\ReflectionClass $class) |
|
62
|
1 |
|
{ |
|
63
|
44 |
|
$methods = $class->getMethods(); |
|
64
|
44 |
|
foreach ($methods as $method) { |
|
65
|
|
|
/* @var $annotation \Ray\Di\Di\PostConstruct|null */ |
|
66
|
28 |
|
$annotation = $this->reader->getMethodAnnotation($method, 'Ray\Di\Di\PostConstruct'); |
|
|
|
|
|
|
67
|
28 |
|
if ($annotation) { |
|
68
|
6 |
|
return $method; |
|
69
|
|
|
} |
|
70
|
44 |
|
} |
|
71
|
|
|
|
|
72
|
44 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
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.