1 | <?php |
||
32 | class ReflectionCompositeFactory extends ReflectorFactory |
||
33 | { |
||
34 | const REFLECTION_OBJECT = ReflectionComposite::class; |
||
35 | |||
36 | const PROPERTIES = [ |
||
37 | 'traits' => [''], |
||
38 | 'interfaces' => [''], |
||
39 | 'Methods' => ['local'], |
||
40 | 'Properties' => ['local', 'required', 'optional', 'built'] |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * @var PHPNativeReflector |
||
45 | */ |
||
46 | protected $reflector; |
||
47 | |||
48 | /** |
||
49 | * @var ReflectionComposite |
||
50 | */ |
||
51 | protected $object; |
||
52 | |||
53 | /** |
||
54 | * @var ReflectionCompositeProviderInterface |
||
55 | */ |
||
56 | protected $provider; |
||
57 | |||
58 | /** |
||
59 | * Creates a new ReflectionCompositeFactory from the given |
||
60 | * classname |
||
61 | * |
||
62 | * @param string $classname The class to build a reflect upon |
||
63 | * @return ReflectionCompositeFactory |
||
64 | */ |
||
65 | 30 | public static function fromClassName(string $classname) |
|
73 | |||
74 | /** |
||
75 | * Constructs the Factory with the given reflector and Composite |
||
76 | * provider |
||
77 | * |
||
78 | * @param PHPNativeReflectionClass $reflect |
||
79 | * @param ReflectionCompositeProviderInterface $provider |
||
80 | */ |
||
81 | 30 | public function __construct |
|
90 | |||
91 | /** |
||
92 | * Builds the ReflectionComposite from the provided parameters |
||
93 | * |
||
94 | * @return ReflectionComposite |
||
95 | */ |
||
96 | 30 | public function build() |
|
97 | { |
||
98 | 30 | $this->initObject(); |
|
99 | |||
100 | 30 | foreach ($this->reflector->getTraits() as $trait) |
|
101 | { |
||
102 | 5 | $this->addInheritance('traits', $trait); |
|
103 | } |
||
104 | |||
105 | 30 | if ($parent = $this->reflector->getParentClass()) |
|
106 | { |
||
107 | 1 | $this->addInheritance('parent', $parent, 'setRawValue'); |
|
108 | } |
||
109 | |||
110 | 30 | foreach ($this->reflector->getInterfaces() as $interface) |
|
111 | { |
||
112 | 1 | $this->addInheritance('interfaces', $interface); |
|
113 | } |
||
114 | |||
115 | 30 | $this->parseDocComment |
|
116 | ([ |
||
117 | 30 | 'generic' => 'addGeneric' |
|
118 | ]); |
||
119 | |||
120 | 30 | $fileName = $this->reflector->getFileName(); |
|
121 | |||
122 | 30 | $file = (new ReflectionFileFactory($fileName))->build(); |
|
123 | 30 | $this->accessor->setRawValue('file', $file); |
|
124 | |||
125 | 30 | $this->accessor->setRawValue |
|
126 | ( |
||
127 | 30 | 'classname', |
|
128 | 30 | $this->reflector->name |
|
129 | ); |
||
130 | 30 | $this->accessor->setRawValue |
|
131 | ( |
||
132 | 30 | 'namespace', |
|
133 | 30 | $file->namespaces[$this->reflector->getNamespaceName()] |
|
134 | ); |
||
135 | |||
136 | 30 | $this->addItems('properties', false, 'Property'); |
|
137 | 30 | $this->addItems('methods', true, 'Method'); |
|
138 | |||
139 | 30 | $this->resizeProperties(); |
|
140 | |||
141 | 30 | return $this->object; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Initialise the object with fixed lists |
||
146 | */ |
||
147 | 30 | protected function initObject() |
|
162 | |||
163 | /** |
||
164 | * Adds a generic value to this class |
||
165 | * |
||
166 | * @var string $name Should be 'generic'. Unused |
||
167 | * @var string $value The annotation value |
||
168 | */ |
||
169 | 27 | public function addGeneric($name, $value) |
|
187 | |||
188 | /** |
||
189 | * Resize the FixedList properties down to their size |
||
190 | */ |
||
191 | 30 | protected function resizeProperties() |
|
201 | |||
202 | /** |
||
203 | * Loops through the list methods or properties adding them to the |
||
204 | * Composite |
||
205 | * |
||
206 | * @param string $name |
||
207 | * @param bool $checkFile |
||
208 | * @param string $singular |
||
209 | */ |
||
210 | 30 | protected function addItems |
|
211 | ( |
||
212 | string $name, |
||
213 | bool $checkFile, |
||
214 | string $signular |
||
215 | ) |
||
216 | { |
||
217 | 30 | foreach ($this->reflector->{'get' . $name}() as $item) |
|
218 | { |
||
219 | // We only reflect on methods in userspace |
||
220 | 30 | if ($checkFile && !$item->getFileName()) |
|
221 | { |
||
222 | continue; |
||
223 | } |
||
224 | // This belongs to a super class, use that definition |
||
225 | // instead |
||
226 | 30 | elseif ($item->class !== $this->reflector->getName()) |
|
227 | { |
||
228 | 1 | $item = $this->provider->get($item->class) |
|
229 | 1 | ->$name[$item->getName()]; |
|
230 | } |
||
231 | // Parse this method |
||
232 | else |
||
233 | { |
||
234 | $factory = |
||
235 | '\Spaark\CompositeUtils\Factory\Reflection' |
||
236 | 30 | . '\Reflection' . $signular . 'Factory'; |
|
237 | 30 | $item = $this->{'build' . $signular} |
|
238 | ( |
||
239 | 30 | new $factory($item), |
|
240 | 30 | $item |
|
241 | ); |
||
242 | 30 | $this->accessor->rawAddToValue |
|
243 | ( |
||
244 | 30 | 'local' . ucfirst($name), |
|
245 | 30 | $item |
|
246 | ); |
||
247 | } |
||
248 | |||
249 | 30 | $this->accessor->getRawValue($name)[$item->name] = $item; |
|
250 | } |
||
251 | 30 | } |
|
252 | |||
253 | /** |
||
254 | * Adds a super class / interface / trait to this Composite |
||
255 | * |
||
256 | * @param string $group The type of superclass (parent, etc...) |
||
257 | * @param PHPNativeReflectionClass $reflect |
||
258 | * @param string $method |
||
259 | */ |
||
260 | 6 | protected function addInheritance |
|
279 | |||
280 | /** |
||
281 | * Uses a ReflectionPropertyFactory to build a ReflectionProperty |
||
282 | * |
||
283 | * @param ReflectionPropertyFactory $factory |
||
284 | * @return ReflectionProperty |
||
285 | */ |
||
286 | 27 | protected function buildProperty |
|
300 | |||
301 | /** |
||
302 | * Uses a ReflectionMethodFactory to build a ReflectionMethod |
||
303 | * |
||
304 | * @param ReflectionMethodFactory $factory |
||
305 | * @return ReflectionMethod |
||
306 | */ |
||
307 | 30 | protected function buildMethod(ReflectionMethodFactory $factory) |
|
312 | } |
||
313 | |||
314 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.