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 NodeTranslationListener 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 NodeTranslationListener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class NodeTranslationListener |
||
28 | { |
||
29 | /** @var SessionInterface|FlashBagInterface */ |
||
30 | private $flashBag; |
||
31 | |||
32 | /** @var LoggerInterface */ |
||
33 | private $logger; |
||
34 | |||
35 | /** @var SlugifierInterface */ |
||
36 | private $slugifier; |
||
37 | |||
38 | /** @var RequestStack */ |
||
39 | private $requestStack; |
||
40 | |||
41 | /** @var DomainConfigurationInterface */ |
||
42 | private $domainConfiguration; |
||
43 | |||
44 | /** @var PagesConfiguration */ |
||
45 | private $pagesConfiguration; |
||
46 | |||
47 | /** |
||
48 | * NodeTranslationListener constructor. |
||
49 | * |
||
50 | * @param SessionInterface|FlashBagInterface $session |
||
|
|||
51 | * @param LoggerInterface $logger |
||
52 | * @param SlugifierInterface $slugifier |
||
53 | * @param RequestStack $requestStack |
||
54 | * @param DomainConfigurationInterface $domainConfiguration |
||
55 | * @param PagesConfiguration $pagesConfiguration |
||
56 | */ |
||
57 | public function __construct( |
||
77 | |||
78 | /** |
||
79 | * @param LifecycleEventArgs $args |
||
80 | */ |
||
81 | View Code Duplication | public function prePersist(LifecycleEventArgs $args) |
|
90 | |||
91 | /** |
||
92 | * @param LifecycleEventArgs $args |
||
93 | */ |
||
94 | View Code Duplication | public function preUpdate(LifecycleEventArgs $args) |
|
103 | |||
104 | /** |
||
105 | * @param NodeTranslation $nodeTranslation |
||
106 | * @param EntityManagerInterface $em |
||
107 | */ |
||
108 | private function setSlugWhenEmpty(NodeTranslation $nodeTranslation, EntityManagerInterface $em) |
||
124 | |||
125 | /** |
||
126 | * @param NodeTranslation $nodeTranslation |
||
127 | */ |
||
128 | private function ensureSlugIsSlugified(NodeTranslation $nodeTranslation) |
||
136 | |||
137 | /** |
||
138 | * OnFlush doctrine event - updates the nodetranslation urls if needed |
||
139 | * |
||
140 | * @param OnFlushEventArgs $args |
||
141 | */ |
||
142 | public function onFlush(OnFlushEventArgs $args) |
||
174 | |||
175 | /** |
||
176 | * Checks if a nodetranslation has children and update their url |
||
177 | * |
||
178 | * @param NodeTranslation $node The node |
||
179 | * @param EntityManagerInterface $em The entity manager |
||
180 | * @param ClassMetadata $class The class meta daat |
||
181 | */ |
||
182 | private function updateNodeChildren(NodeTranslation $node, EntityManagerInterface $em, ClassMetadata $class) |
||
202 | |||
203 | /** |
||
204 | * Update the url for a nodetranslation |
||
205 | * |
||
206 | * @param NodeTranslation $nodeTranslation The node translation |
||
207 | * @param EntityManagerInterface $em The entity manager |
||
208 | * |
||
209 | * @return NodeTranslation|bool returns the node when all is well because it has to be saved |
||
210 | */ |
||
211 | private function updateUrl(NodeTranslation $nodeTranslation, EntityManagerInterface $em) |
||
225 | |||
226 | /** |
||
227 | * A function that checks the URL and sees if it's unique. |
||
228 | * It's allowed to be the same when the node is a StructureNode. |
||
229 | * When a node is deleted it needs to be ignored in the check. |
||
230 | * Offline nodes need to be included as well. |
||
231 | * |
||
232 | * It sluggifies the slug, updates the URL |
||
233 | * and checks all existing NodeTranslations ([1]), excluding itself. If a |
||
234 | * URL existsthat has the same url. If an existing one is found the slug is |
||
235 | * modified, the URL is updated and the check is repeated until no prior |
||
236 | * urls exist. |
||
237 | * |
||
238 | * NOTE: We need a way to tell if the slug has been modified or not. |
||
239 | * NOTE: Would be cool if we could increment a number after the slug. Like |
||
240 | * check if it matches -v# and increment the number. |
||
241 | * |
||
242 | * [1] For all languages for now. The issue is that we need a way to know |
||
243 | * if a node's URL is prepended with the language or not. For now both |
||
244 | * scenarios are possible so we check for all languages. |
||
245 | * |
||
246 | * @param NodeTranslation $translation Reference to the NodeTranslation. |
||
247 | * This is modified in place. |
||
248 | * @param EntityManagerInterface $em The entity manager |
||
249 | * @param array $flashes The flash messages array |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | private function ensureUniqueUrl(NodeTranslation $translation, EntityManagerInterface $em, array $flashes = []) |
||
345 | |||
346 | /** |
||
347 | * Increment a string that ends with a number. |
||
348 | * If the string does not end in a number we'll add the append and then add |
||
349 | * the first number. |
||
350 | * |
||
351 | * @param string $string the string we want to increment |
||
352 | * @param string $append the part we want to append before we start adding |
||
353 | * a number |
||
354 | * |
||
355 | * @return string incremented string |
||
356 | */ |
||
357 | View Code Duplication | private function incrementString($string, $append = '-v') |
|
374 | |||
375 | /** |
||
376 | * @return bool |
||
377 | */ |
||
378 | private function isInRequestScope() |
||
382 | |||
383 | private function getFlashBag() |
||
391 | } |
||
392 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.