Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 1 | final class BlameableSubscriber implements Common\EventSubscriber |
|
36 | { |
||
37 | /** |
||
38 | * Implement nette smart magic |
||
39 | */ |
||
40 | 1 | 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 ORM\Mapping\MappingException |
||
88 | */ |
||
89 | public function loadClassMetadata(ORM\Event\LoadClassMetadataEventArgs $eventArgs) : void |
||
102 | |||
103 | /** |
||
104 | * @param ORM\Event\OnFlushEventArgs $eventArgs |
||
105 | * |
||
106 | * @return void |
||
107 | * |
||
108 | * @throws ORM\Mapping\MappingException |
||
109 | */ |
||
110 | public function onFlush(ORM\Event\OnFlushEventArgs $eventArgs) : void |
||
220 | |||
221 | /** |
||
222 | * @param mixed $entity |
||
223 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
224 | * |
||
225 | * @return void |
||
226 | * |
||
227 | * @throws ORM\Mapping\MappingException |
||
228 | */ |
||
229 | public function prePersist($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
||
243 | |||
244 | /** |
||
245 | * @param mixed $entity |
||
246 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
247 | * |
||
248 | * @return void |
||
249 | * |
||
250 | * @throws ORM\Mapping\MappingException |
||
251 | */ |
||
252 | View Code Duplication | public function preUpdate($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
|
264 | |||
265 | /** |
||
266 | * @param mixed $entity |
||
267 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
||
268 | * |
||
269 | * @return void |
||
270 | * |
||
271 | * @throws ORM\Mapping\MappingException |
||
272 | */ |
||
273 | View Code Duplication | public function preRemove($entity, ORM\Event\LifecycleEventArgs $eventArgs) : void |
|
285 | |||
286 | /** |
||
287 | * Set a custom representation of current user |
||
288 | * |
||
289 | * @param mixed $user |
||
290 | * |
||
291 | * @return void |
||
292 | */ |
||
293 | public function setUser($user) : void |
||
297 | |||
298 | /** |
||
299 | * Get current user, either if $this->user is present or from userCallable |
||
300 | * |
||
301 | * @return mixed The user representation |
||
302 | */ |
||
303 | public function getUser() |
||
317 | |||
318 | /** |
||
319 | * @param callable $callable |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function setUserCallable(callable $callable) : void |
||
327 | |||
328 | /** |
||
329 | * @param array $fields |
||
330 | * @param ORM\UnitOfWork $uow |
||
331 | * @param mixed $object |
||
332 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | private function updateFields(array $fields, ORM\UnitOfWork $uow, $object, ORM\Mapping\ClassMetadata $classMetadata) : void |
||
344 | |||
345 | /** |
||
346 | * Updates a field |
||
347 | * |
||
348 | * @param ORM\UnitOfWork $uow |
||
349 | * @param mixed $object |
||
350 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
351 | * @param string $field |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | private function updateField(ORM\UnitOfWork $uow, $object, ORM\Mapping\ClassMetadata $classMetadata, string $field) : void |
||
369 | |||
370 | /** |
||
371 | * Get the user value to set on a blameable field |
||
372 | * |
||
373 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
374 | * @param string $field |
||
375 | * |
||
376 | * @return mixed |
||
377 | */ |
||
378 | private function getUserValue(ORM\Mapping\ClassMetadata $classMetadata, string $field) |
||
401 | |||
402 | /** |
||
403 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
404 | * @param string $eventName |
||
405 | * |
||
406 | * @return void |
||
407 | * |
||
408 | * @throws ORM\Mapping\MappingException |
||
409 | */ |
||
410 | private function registerEvent(ORM\Mapping\ClassMetadata $classMetadata, string $eventName) : void |
||
416 | |||
417 | /** |
||
418 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
419 | * @param string $eventName |
||
420 | * @param string $listenerClass |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | private static function hasRegisteredListener(ORM\Mapping\ClassMetadata $classMetadata, string $eventName, string $listenerClass) : bool |
||
438 | } |
||
439 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.