Conditions | 30 |
Paths | 648 |
Total Lines | 197 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php /** @noinspection PhpUndefinedClassInspection */ |
||
184 | public function processFile(string $file, string $contents) |
||
185 | { |
||
186 | $this->getLogger()->debug("Processing `$file`"); |
||
187 | $file = realpath($file); |
||
188 | |||
189 | self::$file = $file; |
||
190 | |||
191 | $ignoreFile = sprintf('%s/.signalignore', dirname($file)); |
||
192 | |||
193 | if (file_exists($ignoreFile)) |
||
194 | { |
||
195 | $this->getLogger()->notice("Skipping `$file` because of `$ignoreFile`" . PHP_EOL); |
||
196 | return; |
||
197 | } |
||
198 | |||
199 | $this->paths[] = $file; |
||
200 | // Remove initial `\` from namespace |
||
201 | try |
||
202 | { |
||
203 | $annotated = AnnotationUtility::rawAnnotate($file); |
||
204 | } |
||
205 | catch(NoClassInFileException $e) |
||
206 | { |
||
207 | $this->log($e, $file); |
||
208 | return; |
||
209 | } |
||
210 | catch (ClassNotFoundException $e) |
||
211 | { |
||
212 | $this->log($e, $file); |
||
213 | return; |
||
214 | } |
||
215 | catch (ParseException $e) |
||
216 | { |
||
217 | $this->err($e, $file); |
||
218 | return; |
||
219 | } |
||
220 | catch (UnexpectedValueException $e) |
||
221 | { |
||
222 | $this->err($e, $file); |
||
223 | return; |
||
224 | } |
||
225 | catch (Exception $e) |
||
226 | { |
||
227 | $this->err($e, $file); |
||
228 | return; |
||
229 | } |
||
230 | |||
231 | $namespace = preg_replace('~^\\\\+~', '', $annotated['namespace']); |
||
232 | $className = $annotated['className']; |
||
233 | |||
234 | |||
235 | // Use fully qualified name, class must autoload |
||
236 | $fqn = $namespace . '\\' . $className; |
||
237 | NameNormalizer::normalize($fqn); |
||
238 | |||
239 | try |
||
240 | { |
||
241 | // NOTE: This autoloader must be registered on ReflectionClass |
||
242 | // creation ONLY! That's why register/unregister. |
||
243 | // This will detect not found depending classes |
||
244 | // (base classes,interfaces,traits etc.) |
||
245 | $autoload = static::class . '::autoloadHandler'; |
||
246 | spl_autoload_register($autoload); |
||
247 | eval('$info = new ReflectionClass($fqn);'); |
||
248 | spl_autoload_unregister($autoload); |
||
249 | } |
||
250 | catch (ParseError $e) |
||
251 | { |
||
252 | $this->err($e, $file); |
||
253 | return; |
||
254 | } |
||
255 | catch (ClassNotFoundException $e) |
||
256 | { |
||
257 | $this->log($e, $file); |
||
258 | return; |
||
259 | } |
||
260 | catch (ReflectionException $e) |
||
261 | { |
||
262 | $this->err($e, $file); |
||
263 | return; |
||
264 | } |
||
265 | // $info is created in `eval` |
||
266 | /* @var $info ReflectionClass */ |
||
267 | $isAnnotated = $info->implementsInterface(AnnotatedInterface::class); |
||
268 | $hasSignals = $this->hasSignals($contents); |
||
269 | $isAbstract = $info->isAbstract() || $info->isInterface(); |
||
270 | |||
271 | if ($isAnnotated) |
||
272 | { |
||
273 | $this->getLogger()->debug("Annotated: $info->name"); |
||
274 | } |
||
275 | else |
||
276 | { |
||
277 | $this->getLogger()->debug("Not annotated: $info->name"); |
||
278 | } |
||
279 | |||
280 | // Old classes must now implement interface |
||
281 | // Brake BC! |
||
282 | if ($hasSignals && !$isAnnotated && !$isAbstract) |
||
283 | { |
||
284 | throw new UnexpectedValueException(sprintf('Class %s must implement %s to use signals', $fqn, AnnotatedInterface::class)); |
||
285 | } |
||
286 | |||
287 | // Skip not annotated class |
||
288 | if (!$isAnnotated) |
||
289 | { |
||
290 | return; |
||
291 | } |
||
292 | |||
293 | // Skip abstract classes |
||
294 | if ($isAbstract) |
||
295 | { |
||
296 | return; |
||
297 | } |
||
298 | try |
||
299 | { |
||
300 | // Discard notices (might be the case when outdated cache?) |
||
301 | $level = error_reporting(); |
||
302 | error_reporting(E_WARNING); |
||
303 | $meta = SignalsMeta::create($fqn); |
||
304 | error_reporting($level); |
||
305 | } |
||
306 | catch (ParseException $e) |
||
307 | { |
||
308 | $this->err($e, $file); |
||
309 | return; |
||
310 | } |
||
311 | catch (ClassNotFoundException $e) |
||
312 | { |
||
313 | $this->log($e, $file); |
||
314 | return; |
||
315 | } |
||
316 | catch (UnexpectedValueException $e) |
||
317 | { |
||
318 | $this->err($e, $file); |
||
319 | return; |
||
320 | } |
||
321 | |||
322 | /* @var $typeMeta DocumentTypeMeta */ |
||
323 | $typeMeta = $meta->type(); |
||
324 | |||
325 | // Signals |
||
326 | foreach ($typeMeta->signalFor as $slot) |
||
327 | { |
||
328 | if(is_a($slot, ISignal::class, true)) |
||
329 | { |
||
330 | $this->getLogger()->error(sprintf('Slot `%s` is used as signal on `%s`', $slot, $fqn)); |
||
331 | } |
||
332 | $this->getLogger()->debug("Signal: $slot:$fqn"); |
||
333 | $this->data[Signal::Slots][$slot][$fqn] = true; |
||
334 | } |
||
335 | |||
336 | // Slots |
||
337 | // For constructor injection |
||
338 | foreach ($typeMeta->slotFor as $slot) |
||
339 | { |
||
340 | if(is_a($slot, ISignalSlot::class, true)) |
||
341 | { |
||
342 | $this->getLogger()->error(sprintf('Signal `%s` is used as slot on `%s`', $slot, $fqn)); |
||
343 | } |
||
344 | $key = implode('::', [$fqn, '__construct']) . '()'; |
||
345 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
346 | $this->data[Signal::Signals][$slot][$fqn][$key] = true; |
||
347 | } |
||
348 | |||
349 | // For method injection |
||
350 | foreach ($meta->methods() as $methodName => $method) |
||
351 | { |
||
352 | /* @var $method DocumentMethodMeta */ |
||
353 | foreach ($method->slotFor as $slot) |
||
354 | { |
||
355 | $key = implode('::', [$fqn, $methodName]) . '()'; |
||
356 | if(is_a($slot, ISignalSlot::class, true)) |
||
357 | { |
||
358 | $this->getLogger()->error(sprintf('Signal `%s` is used as slot on `%s`', $slot, $key)); |
||
359 | } |
||
360 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
361 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s()', $methodName); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | // For property injection |
||
366 | foreach ($meta->fields() as $fieldName => $field) |
||
367 | { |
||
368 | /* @var $field DocumentPropertyMeta */ |
||
369 | foreach ($field->slotFor as $slot) |
||
370 | { |
||
371 | $key = implode('::$', [$fqn, $fieldName]); |
||
372 | if(is_a($slot, ISignalSlot::class, true)) |
||
373 | { |
||
374 | $this->getLogger()->error(sprintf('Signal `%s` is used as slot on `%s`', $slot, $key)); |
||
375 | } |
||
376 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
377 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s', $fieldName); |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | |||
426 |