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 | * Will be invoked by the action on the events the listener has been registered for. |
||
50 | * |
||
51 | * @param array $row The row to handle |
||
52 | * |
||
53 | * @return array The modified row |
||
54 | * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
||
55 | */ |
||
56 | public function handle(array $row) |
||
98 | |||
99 | /** |
||
100 | * Prepare's and set's the URL key from the passed row of the CSV file. |
||
101 | * |
||
102 | * @param array $row The row with the CSV data |
||
103 | * |
||
104 | * @return boolean TRUE, if the URL key has been prepared, else FALSE |
||
105 | */ |
||
106 | protected function prepareUrlKey($row) |
||
139 | |||
140 | /** |
||
141 | * Set's the prepared URL key. |
||
142 | * |
||
143 | * @param string $urlKey The prepared URL key |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | protected function setUrlKey($urlKey) |
||
151 | |||
152 | /** |
||
153 | * Return's the prepared URL key. |
||
154 | * |
||
155 | * @return string The prepared URL key |
||
156 | */ |
||
157 | protected function getUrlKey() |
||
161 | |||
162 | /** |
||
163 | * Initialize's and return's the URL key filter. |
||
164 | * |
||
165 | * @return \TechDivision\Import\Product\Utils\ConvertLiteralUrl The URL key filter |
||
166 | */ |
||
167 | protected function getUrlKeyFilter() |
||
171 | |||
172 | /** |
||
173 | * Convert's the passed string into a valid URL key. |
||
174 | * |
||
175 | * @param string $string The string to be converted, e. g. the product name |
||
176 | * |
||
177 | * @return string The converted string as valid URL key |
||
178 | */ |
||
179 | protected function convertNameToUrlKey($string) |
||
183 | |||
184 | /** |
||
185 | * Convert's the passed URL rewrites into an array with the category ID from the |
||
186 | * metadata as key and the URL rewrite as value. |
||
187 | * |
||
188 | * If now category ID can be found in the metadata, the ID of the store's root |
||
189 | * category is used. |
||
190 | * |
||
191 | * @param array $urlRewrites The URL rewrites to convert |
||
192 | * |
||
193 | * @return array The converted array with the de-serialized category IDs as key |
||
194 | */ |
||
195 | protected function prepareExistingCategoryUrlRewrites(array $urlRewrites) |
||
226 | |||
227 | /** |
||
228 | * Remove's the URL rewrites with the passed data. |
||
229 | * |
||
230 | * @param array $existingProductCategoryUrlRewrites The array with the URL rewrites to remove |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | protected function deleteUrlRewrites(array $existingProductCategoryUrlRewrites) |
||
247 | |||
248 | /** |
||
249 | * Create's the URL rewrites from the passed data. |
||
250 | * |
||
251 | * @param array $row The data to create the URL rewrite from |
||
252 | * @param array $productCategoryIds The categories to create a URL rewrite for |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | protected function createUrlRewrites(array $row, array $productCategoryIds) |
||
284 | |||
285 | /** |
||
286 | * Update's existing URL rewrites by creating 301 redirect URL rewrites for each. |
||
287 | * |
||
288 | * @param array $row The row with the actual data |
||
289 | * @param array $existingProductCategoryUrlRewrites The array with the existing URL rewrites |
||
290 | * |
||
291 | * @return void |
||
292 | */ |
||
293 | protected function updateUrlRewrites(array $row, array $existingProductCategoryUrlRewrites) |
||
322 | |||
323 | /** |
||
324 | * Prepare's the target path for a URL rewrite. |
||
325 | * |
||
326 | * @param array $category The categroy with the URL path |
||
327 | * |
||
328 | * @return string The target path |
||
329 | */ |
||
330 | protected function prepareTargetPath(array $category) |
||
349 | |||
350 | /** |
||
351 | * Prepare's the request path for a URL rewrite. |
||
352 | * |
||
353 | * @param array $row The actual row with the data |
||
354 | * @param array $category The categroy with the URL path |
||
355 | * |
||
356 | * @return string The request path |
||
357 | */ |
||
358 | View Code Duplication | protected function prepareRequestPath(array $row, array $category) |
|
377 | |||
378 | /** |
||
379 | * Prepare's the target path for a 301 redirect URL rewrite. |
||
380 | * |
||
381 | * @param array $row The actual row with the data |
||
382 | * @param array $category The categroy with the URL path |
||
383 | * |
||
384 | * @return string The target path |
||
385 | */ |
||
386 | View Code Duplication | protected function prepareTargetPathForRedirect(array $row, array $category) |
|
405 | |||
406 | /** |
||
407 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
408 | * |
||
409 | * @param array $category The category used for preparation |
||
410 | * |
||
411 | * @return array The metadata |
||
412 | */ |
||
413 | protected function prepareMetadata(array $category) |
||
430 | |||
431 | /** |
||
432 | * Set's the store view code the create the product/attributes for. |
||
433 | * |
||
434 | * @param string $storeViewCode The store view code |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | public function setStoreViewCode($storeViewCode) |
||
442 | |||
443 | /** |
||
444 | * Return's the root category for the actual view store. |
||
445 | * |
||
446 | * @return array The store's root category |
||
447 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
448 | */ |
||
449 | public function getRootCategory() |
||
453 | |||
454 | /** |
||
455 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
456 | * |
||
457 | * @param array $category The category to query |
||
458 | * |
||
459 | * @return boolean TRUE if the passed category IS the root category |
||
460 | */ |
||
461 | public function isRootCategory(array $category) |
||
470 | |||
471 | /** |
||
472 | * Return's the list with category IDs the product is related with. |
||
473 | * |
||
474 | * @return array The product's category IDs |
||
475 | */ |
||
476 | public function getProductCategoryIds() |
||
480 | |||
481 | /** |
||
482 | * Return's the category with the passed ID. |
||
483 | * |
||
484 | * @param integer $categoryId The ID of the category to return |
||
485 | * |
||
486 | * @return array The category data |
||
487 | */ |
||
488 | public function getCategory($categoryId) |
||
492 | |||
493 | /** |
||
494 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
495 | * |
||
496 | * @param string $entityType The entity type to load the URL rewrites for |
||
497 | * @param integer $entityId The entity ID to laod the rewrites for |
||
498 | * |
||
499 | * @return array The URL rewrites |
||
500 | */ |
||
501 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
||
505 | |||
506 | /** |
||
507 | * Persist's the URL write with the passed data. |
||
508 | * |
||
509 | * @param array $row The URL rewrite to persist |
||
510 | * |
||
511 | * @return void |
||
512 | */ |
||
513 | public function persistUrlRewrite($row) |
||
517 | |||
518 | /** |
||
519 | * Delete's the URL rewrite with the passed attributes. |
||
520 | * |
||
521 | * @param array $row The attributes of the entity to remove |
||
522 | * |
||
523 | * @return void |
||
524 | */ |
||
525 | public function removeUrlRewrite($row) |
||
529 | } |
||
530 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: