1 | <?php |
||
33 | class Builder |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Cached values of parsing |
||
38 | * @var string[][][] |
||
39 | */ |
||
40 | private static $_cache = []; |
||
41 | |||
42 | /** |
||
43 | * Addendum instance |
||
44 | * @var Addendum |
||
45 | */ |
||
46 | private $addendum = null; |
||
47 | |||
48 | 32 | public function __construct(Addendum $addendum = null) |
|
52 | |||
53 | /** |
||
54 | * Build annotations collection |
||
55 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $targetReflection |
||
56 | * @return AnnotationsCollection |
||
57 | */ |
||
58 | 32 | public function build($targetReflection) |
|
59 | { |
||
60 | 32 | $annotations = []; |
|
61 | 32 | $t = []; |
|
|
|||
62 | |||
63 | |||
64 | |||
65 | // Decide where from take traits |
||
66 | 32 | if ($targetReflection instanceof ReflectionClass) |
|
67 | 32 | { |
|
68 | 32 | $t = $targetReflection->getTraits(); |
|
69 | 32 | } |
|
70 | else |
||
71 | { |
||
72 | 17 | $t = $targetReflection->getDeclaringClass()->getTraits(); |
|
73 | } |
||
74 | |||
75 | // Get annotations from traits |
||
76 | 32 | $traitsData = []; |
|
77 | 32 | foreach ($t as $trait) |
|
78 | { |
||
79 | 2 | $targetTrait = new ReflectionAnnotatedClass($trait->name, $this->addendum); |
|
80 | 2 | $annotationsTrait = null; |
|
81 | |||
82 | // Try to get annotations from entity, be it method, property or trait itself |
||
83 | 3 | switch (true) |
|
84 | 1 | { |
|
85 | 2 | case $targetReflection instanceof \ReflectionProperty && $targetTrait->hasProperty($targetReflection->name): |
|
86 | 2 | $annotationsTrait = new ReflectionAnnotatedProperty($targetTrait->name, $targetReflection->name, $this->addendum); |
|
87 | 2 | break; |
|
88 | 2 | case $targetReflection instanceof \ReflectionMethod && $targetTrait->hasMethod($targetReflection->name): |
|
89 | $annotationsTrait = new ReflectionAnnotatedMethod($targetTrait->name, $targetReflection->name, $this->addendum); |
||
90 | break; |
||
91 | 2 | case $targetReflection instanceof \ReflectionClass: |
|
92 | 2 | $annotationsTrait = $targetTrait; |
|
93 | 2 | break; |
|
94 | } |
||
95 | |||
96 | // Does not have property or method |
||
97 | 2 | if (null === $annotationsTrait) |
|
98 | 2 | { |
|
99 | continue; |
||
100 | } |
||
101 | |||
102 | // Data from traits |
||
103 | 2 | $traitsData = $this->_parse($annotationsTrait); |
|
104 | 32 | } |
|
105 | |||
106 | // Data from class |
||
107 | 32 | $data = $this->_parse($targetReflection); |
|
108 | |||
109 | // Merge data from traits |
||
110 | 32 | $data = array_merge($traitsData, $data); |
|
111 | |||
112 | // Get annotations from current entity |
||
113 | 32 | foreach ($data as $class => $parameters) |
|
114 | { |
||
115 | 30 | foreach ($parameters as $params) |
|
116 | { |
||
117 | 30 | $annotation = $this->instantiateAnnotation($class, $params, $targetReflection); |
|
118 | 30 | if ($annotation !== false) |
|
119 | 30 | { |
|
120 | 30 | $annotations[$class][] = $annotation; |
|
121 | 30 | } |
|
122 | 30 | } |
|
123 | 32 | } |
|
124 | 32 | return new AnnotationsCollection($annotations); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Create new instance of annotation |
||
129 | * @param string $class |
||
130 | * @param mixed[] $parameters |
||
131 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $targetReflection |
||
132 | * @return boolean|object |
||
133 | */ |
||
134 | 30 | public function instantiateAnnotation($class, $parameters, $targetReflection = false) |
|
193 | |||
194 | /** |
||
195 | * Normalize class name and namespace to proper fully qualified name |
||
196 | * @param string $ns |
||
197 | * @param string $class |
||
198 | * @return string |
||
199 | */ |
||
200 | 30 | private function _normalizeFqn($ns, $class) |
|
204 | |||
205 | /** |
||
206 | * Get doc comment |
||
207 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
208 | * @return mixed[] |
||
209 | */ |
||
210 | 32 | private function _parse($reflection) |
|
226 | |||
227 | /** |
||
228 | * Get doc comment |
||
229 | * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection |
||
230 | * @return mixed[] |
||
231 | */ |
||
232 | 28 | protected function getDocComment($reflection) |
|
236 | |||
237 | /** |
||
238 | * Clear local parsing cache |
||
239 | */ |
||
240 | 3 | public static function clearCache() |
|
244 | |||
245 | } |
||
246 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.