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 |
||
35 | class ProductMediaObserver extends AbstractProductImportObserver |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The artefact type. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | const ARTEFACT_TYPE = 'media'; |
||
44 | |||
45 | /** |
||
46 | * The the default image label. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | const DEFAULT_IMAGE_LABEL = 'Image'; |
||
51 | |||
52 | /** |
||
53 | * The default image position. |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | const DEFAULT_IMAGE_POSITION = 0; |
||
58 | |||
59 | /** |
||
60 | * The array with the image information of on row before they'll be converted into artefacts. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $images = array(); |
||
65 | |||
66 | /** |
||
67 | * The image artefacts that has to be exported. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $artefacts = array(); |
||
72 | |||
73 | /** |
||
74 | * The array with names of the images that should be hidden on the product detail page. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $imagesToHide = array(); |
||
79 | |||
80 | /** |
||
81 | * The array with names of the images that has to be disabled. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $disabledImages = array(); |
||
86 | |||
87 | /** |
||
88 | * Holds the image values of the main row. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $mainRow = array(); |
||
93 | |||
94 | /** |
||
95 | * Process the observer's business logic. |
||
96 | * |
||
97 | * @return array The processed row |
||
98 | */ |
||
99 | protected function process() |
||
126 | |||
127 | /** |
||
128 | * Resolve's the value with the passed colum name from the actual row. If a callback will |
||
129 | * be passed, the callback will be invoked with the found value as parameter. If |
||
130 | * the value is NULL or empty, the default value will be returned. |
||
131 | * |
||
132 | * @param string $name The name of the column to return the value for |
||
133 | * @param mixed|null $default The default value, that has to be returned, if the row's value is empty |
||
134 | * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it |
||
135 | * |
||
136 | * @return mixed|null The, almost formatted, value |
||
137 | * @see \TechDivision\Import\Observers\AbstractObserver::getValue() |
||
138 | */ |
||
139 | protected function getImageValue($name, $default = null, callable $callback = null) |
||
152 | |||
153 | /** |
||
154 | * Parses the column and exports the image data to a separate file. |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | protected function processImages() |
||
227 | |||
228 | /** |
||
229 | * Parses the column and exports the additional image data to a separate file. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | protected function processAdditionalImages() |
||
290 | |||
291 | /** |
||
292 | * Load the images that has to be hidden on the product detail page. |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | View Code Duplication | protected function loadImagesToHide() |
|
307 | |||
308 | /** |
||
309 | * Load the images that has to be disabled |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | View Code Duplication | protected function loadImagesToDisable() |
|
324 | |||
325 | /** |
||
326 | * Return's the array with the available image types and their label columns. |
||
327 | * |
||
328 | * @return array The array with the available image types |
||
329 | */ |
||
330 | protected function getImageTypes() |
||
334 | |||
335 | /** |
||
336 | * Return's the default image label. |
||
337 | * |
||
338 | * @return string|null The default image label |
||
339 | * @deprecated Since 23.0.0 |
||
340 | */ |
||
341 | protected function getDefaultImageLabel() |
||
345 | |||
346 | /** |
||
347 | * Returns the default image position. |
||
348 | * |
||
349 | * @return int The default image position |
||
350 | * @deprecated Since 23.0.0 |
||
351 | */ |
||
352 | protected function getDefaultImagePosition() |
||
356 | |||
357 | /** |
||
358 | * Returns the mapped filename (which is the new filename). |
||
359 | * |
||
360 | * @param string $filename The filename to map |
||
361 | * |
||
362 | * @return string The mapped filename |
||
363 | */ |
||
364 | protected function getImageMapping($filename) |
||
368 | |||
369 | /** |
||
370 | * Returns the original filename for passed one (which is the new filename). |
||
371 | * |
||
372 | * @param string $newFilename The new filename to return the original one for |
||
373 | * |
||
374 | * @return string The original filename |
||
375 | */ |
||
376 | protected function getInversedImageMapping($newFilename) |
||
380 | |||
381 | /** |
||
382 | * Returns the media roles of the subject. |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | protected function getMediaRoles() |
||
390 | |||
391 | /** |
||
392 | * Create's and return's a new empty artefact entity. |
||
393 | * |
||
394 | * @param array $columns The array with the column data |
||
395 | * @param array $originalColumnNames The array with a mapping from the old to the new column names |
||
396 | * |
||
397 | * @return array The new artefact entity |
||
398 | */ |
||
399 | protected function newArtefact(array $columns, array $originalColumnNames) |
||
403 | |||
404 | /** |
||
405 | * Add the passed product type artefacts to the product with the |
||
406 | * last entity ID. |
||
407 | * |
||
408 | * @param array $artefacts The product type artefacts |
||
409 | * |
||
410 | * @return void |
||
411 | * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() |
||
412 | */ |
||
413 | protected function addArtefacts(array $artefacts) |
||
417 | } |
||
418 |
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.