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 UrlRewriteObserver 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 UrlRewriteObserver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class UrlRewriteObserver extends AbstractProductImportObserver |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The entity type to load the URL rewrites for. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | const ENTITY_TYPE = 'product'; |
||
47 | |||
48 | /** |
||
49 | * The URL key from the CSV file column that has to be processed by the observer. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $urlKey; |
||
54 | |||
55 | /** |
||
56 | * Will be invoked by the action on the events the listener has been registered for. |
||
57 | * |
||
58 | * @param array $row The row to handle |
||
59 | * |
||
60 | * @return array The modified row |
||
61 | * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
||
62 | */ |
||
63 | 3 | public function handle(array $row) |
|
105 | |||
106 | /** |
||
107 | * Prepare's and set's the URL key from the passed row of the CSV file. |
||
108 | * |
||
109 | * @param array $row The row with the CSV data |
||
110 | * |
||
111 | * @return boolean TRUE, if the URL key has been prepared, else FALSE |
||
112 | */ |
||
113 | 3 | protected function prepareUrlKey($row) |
|
146 | |||
147 | /** |
||
148 | * Set's the prepared URL key. |
||
149 | * |
||
150 | * @param string $urlKey The prepared URL key |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | 3 | protected function setUrlKey($urlKey) |
|
158 | |||
159 | /** |
||
160 | * Return's the prepared URL key. |
||
161 | * |
||
162 | * @return string The prepared URL key |
||
163 | */ |
||
164 | 3 | protected function getUrlKey() |
|
168 | |||
169 | /** |
||
170 | * Initialize's and return's the URL key filter. |
||
171 | * |
||
172 | * @return \TechDivision\Import\Product\Utils\ConvertLiteralUrl The URL key filter |
||
173 | */ |
||
174 | 3 | protected function getUrlKeyFilter() |
|
178 | |||
179 | /** |
||
180 | * Convert's the passed string into a valid URL key. |
||
181 | * |
||
182 | * @param string $string The string to be converted, e. g. the product name |
||
183 | * |
||
184 | * @return string The converted string as valid URL key |
||
185 | */ |
||
186 | 3 | protected function convertNameToUrlKey($string) |
|
190 | |||
191 | /** |
||
192 | * Convert's the passed URL rewrites into an array with the category ID from the |
||
193 | * metadata as key and the URL rewrite as value. |
||
194 | * |
||
195 | * If now category ID can be found in the metadata, the ID of the store's root |
||
196 | * category is used. |
||
197 | * |
||
198 | * @param array $urlRewrites The URL rewrites to convert |
||
199 | * |
||
200 | * @return array The converted array with the de-serialized category IDs as key |
||
201 | */ |
||
202 | 3 | protected function prepareExistingCategoryUrlRewrites(array $urlRewrites) |
|
233 | |||
234 | /** |
||
235 | * Remove's the URL rewrites with the passed data. |
||
236 | * |
||
237 | * @param array $existingProductCategoryUrlRewrites The array with the URL rewrites to remove |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | 3 | protected function deleteUrlRewrites(array $existingProductCategoryUrlRewrites) |
|
254 | |||
255 | /** |
||
256 | * Create's the URL rewrites from the passed data. |
||
257 | * |
||
258 | * @param array $productCategoryIds The categories to create a URL rewrite for |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | 3 | protected function createUrlRewrites(array $productCategoryIds) |
|
287 | |||
288 | /** |
||
289 | * Update's existing URL rewrites by creating 301 redirect URL rewrites for each. |
||
290 | * |
||
291 | * @param array $existingProductCategoryUrlRewrites The array with the existing URL rewrites |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 3 | protected function updateUrlRewrites(array $existingProductCategoryUrlRewrites) |
|
321 | |||
322 | /** |
||
323 | * Prepare's the target path for a URL rewrite. |
||
324 | * |
||
325 | * @param array $category The categroy with the URL path |
||
326 | * |
||
327 | * @return string The target path |
||
328 | */ |
||
329 | 3 | protected function prepareTargetPath(array $category) |
|
348 | |||
349 | /** |
||
350 | * Prepare's the request path for a URL rewrite. |
||
351 | * |
||
352 | * @param array $category The categroy with the URL path |
||
353 | * |
||
354 | * @return string The request path |
||
355 | */ |
||
356 | 3 | View Code Duplication | protected function prepareRequestPath(array $category) |
372 | |||
373 | /** |
||
374 | * Prepare's the target path for a 301 redirect URL rewrite. |
||
375 | * |
||
376 | * @param array $category The categroy with the URL path |
||
377 | * |
||
378 | * @return string The target path |
||
379 | */ |
||
380 | 2 | View Code Duplication | protected function prepareTargetPathForRedirect(array $category) |
396 | |||
397 | /** |
||
398 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
399 | * |
||
400 | * @param array $category The category used for preparation |
||
401 | * |
||
402 | * @return array The metadata |
||
403 | */ |
||
404 | 3 | protected function prepareMetadata(array $category) |
|
421 | |||
422 | /** |
||
423 | * Set's the store view code the create the product/attributes for. |
||
424 | * |
||
425 | * @param string $storeViewCode The store view code |
||
426 | * |
||
427 | * @return void |
||
428 | */ |
||
429 | 3 | public function setStoreViewCode($storeViewCode) |
|
433 | |||
434 | /** |
||
435 | * Return's the root category for the actual view store. |
||
436 | * |
||
437 | * @return array The store's root category |
||
438 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
439 | */ |
||
440 | 3 | public function getRootCategory() |
|
444 | |||
445 | /** |
||
446 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
447 | * |
||
448 | * @param array $category The category to query |
||
449 | * |
||
450 | * @return boolean TRUE if the passed category IS the root category |
||
451 | */ |
||
452 | 3 | public function isRootCategory(array $category) |
|
461 | |||
462 | /** |
||
463 | * Return's the list with category IDs the product is related with. |
||
464 | * |
||
465 | * @return array The product's category IDs |
||
466 | */ |
||
467 | 4 | public function getProductCategoryIds() |
|
471 | |||
472 | /** |
||
473 | * Return's the category with the passed ID. |
||
474 | * |
||
475 | * @param integer $categoryId The ID of the category to return |
||
476 | * |
||
477 | * @return array The category data |
||
478 | */ |
||
479 | 3 | public function getCategory($categoryId) |
|
483 | |||
484 | /** |
||
485 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
486 | * |
||
487 | * @param string $entityType The entity type to load the URL rewrites for |
||
488 | * @param integer $entityId The entity ID to laod the rewrites for |
||
489 | * |
||
490 | * @return array The URL rewrites |
||
491 | */ |
||
492 | 4 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
496 | |||
497 | /** |
||
498 | * Persist's the URL write with the passed data. |
||
499 | * |
||
500 | * @param array $row The URL rewrite to persist |
||
501 | * |
||
502 | * @return void |
||
503 | */ |
||
504 | 4 | public function persistUrlRewrite($row) |
|
508 | |||
509 | /** |
||
510 | * Delete's the URL rewrite with the passed attributes. |
||
511 | * |
||
512 | * @param array $row The attributes of the entity to remove |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | 3 | public function removeUrlRewrite($row) |
|
520 | } |
||
521 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.