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:
1 | <?php |
||
37 | class UrlRewriteObserver extends AbstractProductImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The entity type to load the URL rewrites for. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const ENTITY_TYPE = 'product'; |
||
46 | |||
47 | /** |
||
48 | * The URL key from the CSV file column that has to be processed by the observer. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $urlKey; |
||
53 | |||
54 | /** |
||
55 | * The actual category ID to process. |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | protected $categoryId; |
||
60 | |||
61 | /** |
||
62 | * The actual entity ID to process. |
||
63 | * |
||
64 | * @var integer |
||
65 | */ |
||
66 | protected $entityId; |
||
67 | |||
68 | /** |
||
69 | * The array with the URL rewrites that has to be created. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $urlRewrites = array(); |
||
74 | |||
75 | /** |
||
76 | * Will be invoked by the action on the events the listener has been registered for. |
||
77 | * |
||
78 | * @param array $row The row to handle |
||
79 | * |
||
80 | * @return array The modified row |
||
81 | * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
||
82 | */ |
||
83 | 3 | View Code Duplication | public function handle(array $row) |
100 | |||
101 | /** |
||
102 | * Set's the prepared URL key. |
||
103 | * |
||
104 | * @param string $urlKey The prepared URL key |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | 3 | protected function setUrlKey($urlKey) |
|
112 | |||
113 | /** |
||
114 | * Return's the prepared URL key. |
||
115 | * |
||
116 | * @return string The prepared URL key |
||
117 | */ |
||
118 | 3 | protected function getUrlKey() |
|
122 | |||
123 | /** |
||
124 | * Set's the actual category ID to process. |
||
125 | * |
||
126 | * @param integer $categoryId The category ID |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | 3 | protected function setCategoryId($categoryId) |
|
134 | |||
135 | /** |
||
136 | * Return's the actual category ID to process. |
||
137 | * |
||
138 | * @return integer The category ID |
||
139 | */ |
||
140 | 3 | protected function getCategoryId() |
|
144 | |||
145 | /** |
||
146 | * Set's the actual entity ID to process. |
||
147 | * |
||
148 | * @param integer $entityId The entity ID |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | 3 | protected function setEntityId($entityId) |
|
156 | |||
157 | /** |
||
158 | * Return's the actual entity ID to process. |
||
159 | * |
||
160 | * @return integer The entity ID |
||
161 | */ |
||
162 | 3 | protected function getEntityId() |
|
166 | |||
167 | /** |
||
168 | * Process the observer's business logic. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | 3 | protected function process() |
|
173 | { |
||
174 | |||
175 | // try to prepare the URL key, return immediately if not possible |
||
176 | 3 | if (!$this->prepareUrlKey()) { |
|
177 | return; |
||
178 | } |
||
179 | |||
180 | // initialize the store view code |
||
181 | 3 | $this->prepareStoreViewCode(); |
|
182 | |||
183 | // prepare the URL rewrites |
||
184 | 3 | $this->prepareUrlRewrites(); |
|
185 | |||
186 | // iterate over the categories and create the URL rewrites |
||
187 | 3 | foreach ($this->urlRewrites as $urlRewrite) { |
|
188 | // initialize and persist the URL rewrite |
||
189 | 3 | if ($urlRewrite = $this->initializeUrlRewrite($urlRewrite)) { |
|
190 | 3 | $this->persistUrlRewrite($urlRewrite); |
|
191 | 3 | } |
|
192 | 3 | } |
|
193 | 3 | } |
|
194 | |||
195 | /** |
||
196 | * Initialize the category product with the passed attributes and returns an instance. |
||
197 | * |
||
198 | * @param array $attr The category product attributes |
||
199 | * |
||
200 | * @return array The initialized category product |
||
201 | */ |
||
202 | 2 | protected function initializeUrlRewrite(array $attr) |
|
206 | |||
207 | /** |
||
208 | * Prepare's the URL rewrites that has to be created/updated. |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | 3 | protected function prepareUrlRewrites() |
|
235 | |||
236 | /** |
||
237 | * Prepare's and set's the URL key from the passed row of the CSV file. |
||
238 | * |
||
239 | * @return boolean TRUE, if the URL key has been prepared, else FALSE |
||
240 | */ |
||
241 | 3 | protected function prepareUrlKey() |
|
276 | |||
277 | /** |
||
278 | * Prepare the attributes of the entity that has to be persisted. |
||
279 | * |
||
280 | * @return array The prepared attributes |
||
281 | */ |
||
282 | 3 | protected function prepareAttributes() |
|
315 | |||
316 | /** |
||
317 | * Prepare's the target path for a URL rewrite. |
||
318 | * |
||
319 | * @param array $category The categroy with the URL path |
||
320 | * |
||
321 | * @return string The target path |
||
322 | */ |
||
323 | 3 | protected function prepareTargetPath(array $category) |
|
339 | |||
340 | /** |
||
341 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
342 | * |
||
343 | * @param array $category The categroy with the URL path |
||
344 | * |
||
345 | * @return string The request path |
||
346 | */ |
||
347 | 3 | protected function prepareRequestPath(array $category) |
|
360 | |||
361 | /** |
||
362 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
363 | * |
||
364 | * @param array $category The category used for preparation |
||
365 | * |
||
366 | * @return array The metadata |
||
367 | */ |
||
368 | 3 | protected function prepareMetadata(array $category) |
|
385 | |||
386 | /** |
||
387 | * Initialize's and return's the URL key filter. |
||
388 | * |
||
389 | * @return \TechDivision\Import\Product\Utils\ConvertLiteralUrl The URL key filter |
||
390 | */ |
||
391 | protected function getUrlKeyFilter() |
||
395 | |||
396 | /** |
||
397 | * Convert's the passed string into a valid URL key. |
||
398 | * |
||
399 | * @param string $string The string to be converted, e. g. the product name |
||
400 | * |
||
401 | * @return string The converted string as valid URL key |
||
402 | */ |
||
403 | protected function convertNameToUrlKey($string) |
||
407 | |||
408 | /** |
||
409 | * Return's the root category for the actual view store. |
||
410 | * |
||
411 | * @return array The store's root category |
||
412 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
413 | */ |
||
414 | 3 | protected function getRootCategory() |
|
418 | |||
419 | /** |
||
420 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
421 | * |
||
422 | * @param array $category The category to query |
||
423 | * |
||
424 | * @return boolean TRUE if the passed category IS the root category |
||
425 | */ |
||
426 | 3 | protected function isRootCategory(array $category) |
|
435 | |||
436 | /** |
||
437 | * Return's the store ID of the actual row, or of the default store |
||
438 | * if no store view code is set in the CSV file. |
||
439 | * |
||
440 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
441 | * |
||
442 | * @return integer The ID of the actual store |
||
443 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
444 | */ |
||
445 | 3 | protected function getRowStoreId($default = null) |
|
449 | |||
450 | /** |
||
451 | * Return's the list with category IDs the product is related with. |
||
452 | * |
||
453 | * @return array The product's category IDs |
||
454 | */ |
||
455 | 3 | protected function getProductCategoryIds() |
|
459 | |||
460 | /** |
||
461 | * Return's the category with the passed ID. |
||
462 | * |
||
463 | * @param integer $categoryId The ID of the category to return |
||
464 | * |
||
465 | * @return array The category data |
||
466 | */ |
||
467 | 3 | protected function getCategory($categoryId) |
|
471 | |||
472 | /** |
||
473 | * Persist's the URL write with the passed data. |
||
474 | * |
||
475 | * @param array $row The URL rewrite to persist |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | 3 | protected function persistUrlRewrite($row) |
|
483 | |||
484 | /** |
||
485 | * Return's the PK to create the product => attribute relation. |
||
486 | * |
||
487 | * @return integer The PK to create the relation with |
||
488 | */ |
||
489 | 3 | protected function getPrimaryKey() |
|
493 | } |
||
494 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.