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 BlameableListener 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 BlameableListener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | 1 | class BlameableListener extends Nette\Object implements Events\Subscriber |
|
41 | { |
||
42 | /** |
||
43 | * Define class name |
||
44 | */ |
||
45 | const CLASS_NAME = __CLASS__; |
||
46 | |||
47 | /** |
||
48 | * @var callable |
||
49 | */ |
||
50 | private $userCallable; |
||
51 | |||
52 | /** |
||
53 | * @var mixed |
||
54 | */ |
||
55 | private $user; |
||
56 | |||
57 | /** |
||
58 | * @var Mapping\Driver\Blameable |
||
59 | */ |
||
60 | private $driver; |
||
61 | |||
62 | /** |
||
63 | * Register events |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getSubscribedEvents() |
||
74 | |||
75 | /** |
||
76 | * @param callable|NULL $userCallable |
||
77 | * @param Mapping\Driver\Blameable $driver |
||
78 | */ |
||
79 | public function __construct( |
||
86 | |||
87 | /** |
||
88 | * @param ORM\Event\LoadClassMetadataEventArgs $eventArgs |
||
89 | * |
||
90 | * @throws Exceptions\InvalidMappingException |
||
91 | */ |
||
92 | public function loadClassMetadata(ORM\Event\LoadClassMetadataEventArgs $eventArgs) |
||
93 | { |
||
94 | /** @var ORM\Mapping\ClassMetadata $classMetadata */ |
||
95 | 1 | $classMetadata = $eventArgs->getClassMetadata(); |
|
96 | 1 | $this->driver->loadMetadataForObjectClass($classMetadata); |
|
97 | |||
98 | // Register pre persist event |
||
99 | 1 | $this->registerEvent($classMetadata, ORM\Events::prePersist); |
|
100 | // Register pre update event |
||
101 | 1 | $this->registerEvent($classMetadata, ORM\Events::preUpdate); |
|
102 | // Register pre remove event |
||
103 | 1 | $this->registerEvent($classMetadata, ORM\Events::preRemove); |
|
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * @param ORM\Event\OnFlushEventArgs $eventArgs |
||
108 | * |
||
109 | * @throws Exceptions\UnexpectedValueException |
||
110 | */ |
||
111 | public function onFlush(ORM\Event\OnFlushEventArgs $eventArgs) |
||
221 | 1 | ||
222 | 1 | /** |
|
223 | 1 | * @param mixed $entity |
|
224 | 1 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
|
225 | 1 | */ |
|
226 | 1 | public function prePersist($entity, Doctrine\ORM\Event\LifecycleEventArgs $eventArgs) |
|
240 | 1 | ||
241 | /** |
||
242 | 1 | * @param mixed $entity |
|
243 | 1 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
|
244 | 1 | */ |
|
245 | 1 | View Code Duplication | public function preUpdate($entity, Doctrine\ORM\Event\LifecycleEventArgs $eventArgs) |
257 | |||
258 | /** |
||
259 | 1 | * @param mixed $entity |
|
260 | 1 | * @param ORM\Event\LifecycleEventArgs $eventArgs |
|
261 | 1 | */ |
|
262 | View Code Duplication | public function preRemove($entity, Doctrine\ORM\Event\LifecycleEventArgs $eventArgs) |
|
274 | |||
275 | /** |
||
276 | * Set a custom representation of current user |
||
277 | * |
||
278 | * @param mixed $user |
||
279 | */ |
||
280 | public function setUser($user) |
||
284 | |||
285 | /** |
||
286 | * Get current user, either if $this->user is present or from userCallable |
||
287 | * |
||
288 | * @return mixed The user representation |
||
289 | */ |
||
290 | public function getUser() |
||
304 | |||
305 | /** |
||
306 | * @param callable $callable |
||
307 | */ |
||
308 | public function setUserCallable(callable $callable) |
||
312 | |||
313 | /** |
||
314 | * @param array $fields |
||
315 | * @param ORM\UnitOfWork $uow |
||
316 | * @param mixed $object |
||
317 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
318 | */ |
||
319 | private function updateFields(array $fields, ORM\UnitOfWork $uow, $object, ORM\Mapping\ClassMetadata $classMetadata) |
||
337 | |||
338 | /** |
||
339 | * Get the user value to set on a blameable field |
||
340 | * |
||
341 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
342 | * @param string $field |
||
343 | * |
||
344 | * @return mixed |
||
345 | 1 | */ |
|
346 | private function getUserValue(ORM\Mapping\ClassMetadata $classMetadata, $field) |
||
369 | |||
370 | /** |
||
371 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
372 | * @param string $eventName |
||
373 | * |
||
374 | * @throws ORM\Mapping\MappingException |
||
375 | 1 | */ |
|
376 | 1 | private function registerEvent(ORM\Mapping\ClassMetadata $classMetadata, $eventName) |
|
382 | |||
383 | /** |
||
384 | * @param ORM\Mapping\ClassMetadata $classMetadata |
||
385 | * @param string $eventName |
||
386 | * @param string $listenerClass |
||
387 | * |
||
388 | * @return bool |
||
389 | 1 | */ |
|
390 | 1 | private static function hasRegisteredListener(ORM\Mapping\ClassMetadata $classMetadata, $eventName, $listenerClass) |
|
404 | } |
||
405 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.