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 | */ |
||
52 | public function __construct( |
||
72 | |||
73 | View Code Duplication | public function prePersist(LifecycleEventArgs $args) |
|
82 | |||
83 | View Code Duplication | public function preUpdate(LifecycleEventArgs $args) |
|
92 | |||
93 | private function setSlugWhenEmpty(NodeTranslation $nodeTranslation, EntityManagerInterface $em) |
||
109 | |||
110 | private function ensureSlugIsSlugified(NodeTranslation $nodeTranslation) |
||
118 | |||
119 | /** |
||
120 | * OnFlush doctrine event - updates the nodetranslation urls if needed |
||
121 | */ |
||
122 | public function onFlush(OnFlushEventArgs $args) |
||
154 | |||
155 | /** |
||
156 | * Checks if a nodetranslation has children and update their url |
||
157 | * |
||
158 | * @param NodeTranslation $node The node |
||
159 | * @param EntityManagerInterface $em The entity manager |
||
160 | * @param ClassMetadata $class The class meta daat |
||
161 | */ |
||
162 | private function updateNodeChildren(NodeTranslation $node, EntityManagerInterface $em, ClassMetadata $class) |
||
182 | |||
183 | /** |
||
184 | * Update the url for a nodetranslation |
||
185 | * |
||
186 | * @param NodeTranslation $nodeTranslation The node translation |
||
187 | * @param EntityManagerInterface $em The entity manager |
||
188 | * |
||
189 | * @return NodeTranslation|bool returns the node when all is well because it has to be saved |
||
190 | */ |
||
191 | private function updateUrl(NodeTranslation $nodeTranslation, EntityManagerInterface $em) |
||
205 | |||
206 | /** |
||
207 | * A function that checks the URL and sees if it's unique. |
||
208 | * It's allowed to be the same when the node is a StructureNode. |
||
209 | * When a node is deleted it needs to be ignored in the check. |
||
210 | * Offline nodes need to be included as well. |
||
211 | * |
||
212 | * It sluggifies the slug, updates the URL |
||
213 | * and checks all existing NodeTranslations ([1]), excluding itself. If a |
||
214 | * URL existsthat has the same url. If an existing one is found the slug is |
||
215 | * modified, the URL is updated and the check is repeated until no prior |
||
216 | * urls exist. |
||
217 | * |
||
218 | * NOTE: We need a way to tell if the slug has been modified or not. |
||
219 | * NOTE: Would be cool if we could increment a number after the slug. Like |
||
220 | * check if it matches -v# and increment the number. |
||
221 | * |
||
222 | * [1] For all languages for now. The issue is that we need a way to know |
||
223 | * if a node's URL is prepended with the language or not. For now both |
||
224 | * scenarios are possible so we check for all languages. |
||
225 | * |
||
226 | * @param NodeTranslation $translation Reference to the NodeTranslation. |
||
227 | * This is modified in place. |
||
228 | * @param EntityManagerInterface $em The entity manager |
||
229 | * @param array $flashes The flash messages array |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | private function ensureUniqueUrl(NodeTranslation $translation, EntityManagerInterface $em, array $flashes = []) |
||
325 | |||
326 | /** |
||
327 | * Increment a string that ends with a number. |
||
328 | * If the string does not end in a number we'll add the append and then add |
||
329 | * the first number. |
||
330 | * |
||
331 | * @param string $string the string we want to increment |
||
332 | * @param string $append the part we want to append before we start adding |
||
333 | * a number |
||
334 | * |
||
335 | * @return string incremented string |
||
336 | */ |
||
337 | View Code Duplication | private function incrementString($string, $append = '-v') |
|
354 | |||
355 | /** |
||
356 | * @return bool |
||
357 | */ |
||
358 | private function isInRequestScope() |
||
362 | |||
363 | private function getFlashBag() |
||
371 | } |
||
372 |
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.