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 |
||
41 | class MediaSubject extends AbstractProductSubject implements FileUploadSubjectInterface, CleanUpColumnsSubjectInterface, ExportableSubjectInterface |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * The trait that provides file upload functionality. |
||
46 | * |
||
47 | * @var \TechDivision\Import\Subjects\FileUploadTrait |
||
48 | */ |
||
49 | use FileUploadTrait; |
||
50 | |||
51 | /** |
||
52 | * The trait that provides media import functionality. |
||
53 | * |
||
54 | * @var \TechDivision\Import\Product\Media\Subjects\MediaSubjectTrait |
||
55 | */ |
||
56 | use MediaSubjectTrait; |
||
57 | |||
58 | /** |
||
59 | * The trait with the exportable functionality. |
||
60 | * |
||
61 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
62 | */ |
||
63 | use ExportableTrait; |
||
64 | |||
65 | /** |
||
66 | * Intializes the previously loaded global data for exactly one variants. |
||
67 | * |
||
68 | * @param string $serial The serial of the actual import |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public function setUp($serial) |
||
73 | { |
||
74 | |||
75 | // invoke parent method |
||
76 | parent::setUp($serial); |
||
77 | |||
78 | // load the entity manager and the registry processor |
||
79 | $registryProcessor = $this->getRegistryProcessor(); |
||
80 | |||
81 | // load the status of the actual import process |
||
82 | $status = $registryProcessor->getAttribute(RegistryKeys::STATUS); |
||
83 | |||
84 | // load the SKU => entity ID mapping |
||
85 | $this->skuEntityIdMapping = isset($status[RegistryKeys::SKU_ENTITY_ID_MAPPING]) ? $status[RegistryKeys::SKU_ENTITY_ID_MAPPING] : array(); |
||
86 | |||
87 | // initialize media directory => can be absolute or relative |
||
88 | View Code Duplication | if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
|
|
|||
89 | try { |
||
90 | $this->setMediaDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY))); |
||
91 | } catch (\InvalidArgumentException $iae) { |
||
92 | $this->getSystemLogger()->warning($iae->getMessage()); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // initialize images directory => can be absolute or relative |
||
97 | View Code Duplication | if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
|
98 | try { |
||
99 | $this->setImagesFileDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY))); |
||
100 | } catch (\InvalidArgumentException $iae) { |
||
101 | $this->getSystemLogger()->warning($iae->getMessage()); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Merge the columns from the configuration with all image type columns to define which |
||
108 | * columns should be cleaned-up. |
||
109 | * |
||
110 | * @return array The columns that has to be cleaned-up |
||
111 | */ |
||
112 | public function getCleanUpColumns() |
||
126 | } |
||
127 |
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.