Complex classes like BlameableSubscriber often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BlameableSubscriber, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | final class BlameableSubscriber implements Common\EventSubscriber |
||
36 | { |
||
37 | /** |
||
38 | * Implement nette smart magic |
||
39 | */ |
||
40 | use Nette\SmartObject; |
||
41 | |||
42 | /** |
||
43 | * @var callable |
||
44 | */ |
||
45 | private $userCallable; |
||
46 | |||
47 | /** |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $user; |
||
51 | |||
52 | /** |
||
53 | * @var Mapping\Driver\Blameable |
||
54 | */ |
||
55 | private $driver; |
||
56 | |||
57 | /** |
||
58 | * Register events |
||
59 | * |
||
60 | * @return string[] |
||
61 | */ |
||
62 | public function getSubscribedEvents() : array |
||
69 | |||
70 | /** |
||
71 | * @param callable|NULL $userCallable |
||
72 | * @param Mapping\Driver\Blameable $driver |
||
73 | */ |
||
74 | public function __construct( |
||
81 | |||
82 | /** |
||
83 | * @param ORM\Event\LoadClassMetadataEventArgs $eventArgs |
||
84 | * |
||
85 | * @return void |
||
86 | * |
||
87 | * @throws Common\Annotations\AnnotationException |
||
88 | * @throws ORM\Mapping\MappingException |
||
89 | */ |
||
90 | public function loadClassMetadata(ORM\Event\LoadClassMetadataEventArgs $eventArgs) : void |
||
103 | |||
104 | /** |
||
105 | * @param ORM\Event\OnFlushEventArgs $eventArgs |
||
106 | * |
||
107 | * @return void |
||
108 | * |
||
109 | * @throws Common\Annotations\AnnotationException |
||
110 | * @throws ORM\Mapping\MappingException |
||
111 | */ |
||
112 | public function onFlush(ORM\Event\OnFlushEventArgs $eventArgs) : void |
||
222 | |||
223 | /** |
||
224 | * @param mixed $entity |
||
225 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
226 | * |
||
227 | * @return void |
||
228 | * |
||
229 | * @throws Common\Annotations\AnnotationException |
||
230 | * @throws ORM\Mapping\MappingException |
||
231 | */ |
||
232 | public function prePersist($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
||
246 | |||
247 | /** |
||
248 | * @param mixed $entity |
||
249 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
250 | * |
||
251 | * @return void |
||
252 | * |
||
253 | * @throws Common\Annotations\AnnotationException |
||
254 | * @throws ORM\Mapping\MappingException |
||
255 | */ |
||
256 | public function preUpdate($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
||
268 | |||
269 | /** |
||
270 | * @param mixed $entity |
||
271 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
272 | * |
||
273 | * @return void |
||
274 | * |
||
275 | * @throws Common\Annotations\AnnotationException |
||
276 | * @throws ORM\Mapping\MappingException |
||
277 | */ |
||
278 | public function preRemove($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
||
290 | |||
291 | /** |
||
292 | * Set a custom representation of current user |
||
293 | * |
||
294 | * @param mixed $user |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | public function setUser($user) : void |
||
302 | |||
303 | /** |
||
304 | * Get current user, either if $this->user is present or from userCallable |
||
305 | * |
||
306 | * @return mixed The user representation |
||
307 | */ |
||
308 | public function getUser() |
||
322 | |||
323 | /** |
||
324 | * @param callable $callable |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function setUserCallable(callable $callable) : void |
||
332 | |||
333 | /** |
||
334 | * @param array $fields |
||
335 | * @param ORM\UnitOfWork $uow |
||
336 | * @param mixed $object |
||
337 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | private function updateFields(array $fields, ORM\UnitOfWork $uow, $object, ORM\Mapping\ClassMetadata $classMetadata) : void |
||
349 | |||
350 | /** |
||
351 | * Updates a field |
||
352 | * |
||
353 | * @param ORM\UnitOfWork $uow |
||
354 | * @param mixed $object |
||
355 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
356 | * @param string $field |
||
357 | * |
||
358 | * @return void |
||
359 | */ |
||
360 | private function updateField(ORM\UnitOfWork $uow, $object, ORM\Mapping\ClassMetadata $classMetadata, string $field) : void |
||
374 | |||
375 | /** |
||
376 | * Get the user value to set on a blameable field |
||
377 | * |
||
378 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
379 | * @param string $field |
||
380 | * |
||
381 | * @return mixed |
||
382 | */ |
||
383 | private function getUserValue(ORM\Mapping\ClassMetadata $classMetadata, string $field) |
||
406 | |||
407 | /** |
||
408 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
409 | * @param string $eventName |
||
410 | * |
||
411 | * @return void |
||
412 | * |
||
413 | * @throws ORM\Mapping\MappingException |
||
414 | */ |
||
415 | private function registerEvent(ORM\Mapping\ClassMetadata $classMetadata, string $eventName) : void |
||
421 | |||
422 | /** |
||
423 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
424 | * @param string $eventName |
||
425 | * @param string $listenerClass |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | private static function hasRegisteredListener(ORM\Mapping\ClassMetadata $classMetadata, string $eventName, string $listenerClass) : bool |
||
443 | } |
||
444 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.