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 | trait ExportableTrait |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The array containing the data for product type configuration (configurables, bundles, etc). |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $artefacts = array(); |
||
44 | |||
45 | /** |
||
46 | * The export adapter instance. |
||
47 | * |
||
48 | * @var \TechDivision\Import\Adapter\ExportAdapterInterface |
||
49 | */ |
||
50 | protected $exportAdapter; |
||
51 | |||
52 | /** |
||
53 | * The ID of the product that has been created recently. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $lastEntityId; |
||
58 | |||
59 | /** |
||
60 | * Return's the artefacts for post-processing. |
||
61 | * |
||
62 | * @return array The artefacts |
||
63 | */ |
||
64 | 4 | public function getArtefacts() |
|
68 | |||
69 | /** |
||
70 | * Reset the array with the artefacts to free the memory. |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | 1 | protected function resetArtefacts() |
|
78 | |||
79 | /** |
||
80 | * Add the passed product type artefacts to the product with the |
||
81 | * last entity ID. |
||
82 | * |
||
83 | * @param string $type The artefact type, e. g. configurable |
||
84 | * @param array $artefacts The product type artefacts |
||
85 | * @param boolean $override Whether or not the artefacts for the actual entity ID has to be overwritten |
||
86 | * |
||
87 | * @return void |
||
88 | * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId() |
||
89 | */ |
||
90 | 5 | public function addArtefacts($type, array $artefacts, $override = true) |
|
113 | |||
114 | /** |
||
115 | * Add the passed product type artefacts to the product with the |
||
116 | * last entity ID and overrides existing ones with the same key. |
||
117 | * |
||
118 | * @param string $type The artefact type, e. g. configurable |
||
119 | * @param array $artefacts The product type artefacts |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 4 | protected function overrideArtefacts($type, array $artefacts) |
|
129 | |||
130 | /** |
||
131 | * Append's the passed product type artefacts to the product with the |
||
132 | * last entity ID. |
||
133 | * |
||
134 | * @param string $type The artefact type, e. g. configurable |
||
135 | * @param array $artefacts The product type artefacts |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | protected function appendArtefacts($type, array $artefacts) |
||
145 | |||
146 | /** |
||
147 | * Return the artefacts for the passed type and entity ID. |
||
148 | * |
||
149 | * @param string $type The artefact type, e. g. configurable |
||
150 | * @param string $entityId The entity ID to return the artefacts for |
||
151 | * |
||
152 | * @return array The array with the artefacts |
||
153 | * @throws \Exception Is thrown, if no artefacts are available |
||
154 | */ |
||
155 | 2 | public function getArtefactsByTypeAndEntityId($type, $entityId) |
|
156 | { |
||
157 | |||
158 | // query whether or not, artefacts for the passed params are available |
||
159 | 2 | if (isset($this->artefacts[$type][$entityId])) { |
|
160 | // load the artefacts |
||
161 | 1 | $artefacts = $this->artefacts[$type][$entityId]; |
|
162 | |||
163 | // unserialize the original data, if we're in debug mode, if we're in debug mode |
||
164 | 1 | $keys = array_keys($artefacts); |
|
165 | 1 | View Code Duplication | foreach ($keys as $key) { |
166 | 1 | if (isset($artefacts[$key][ColumnKeys::ORIGINAL_DATA])) { |
|
167 | $artefacts[$key][ColumnKeys::ORIGINAL_DATA] = $this->isDebugMode() ? unserialize($artefacts[$key][ColumnKeys::ORIGINAL_DATA]) : null; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // return the artefacts |
||
172 | 1 | return $artefacts; |
|
173 | } |
||
174 | |||
175 | // throw an exception if not |
||
176 | 1 | throw new \Exception( |
|
177 | 1 | sprintf( |
|
178 | 1 | 'Cant\'t load artefacts for type %s and entity ID %d', |
|
179 | 1 | $type, |
|
180 | 1 | $entityId |
|
181 | ) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Queries whether or not artefacts for the passed type and entity ID are available. |
||
187 | * |
||
188 | * @param string $type The artefact type, e. g. configurable |
||
189 | * @param string $entityId The entity ID to return the artefacts for |
||
190 | * |
||
191 | * @return boolean TRUE if artefacts are available, else FALSE |
||
192 | */ |
||
193 | public function hasArtefactsByTypeAndEntityId($type, $entityId) |
||
197 | |||
198 | /** |
||
199 | * Create's and return's a new empty artefact entity. |
||
200 | * |
||
201 | * @param array $columns The array with the column data |
||
202 | * @param array $originalColumnNames The array with a mapping from the old to the new column names |
||
203 | * |
||
204 | * @return array The new artefact entity |
||
205 | */ |
||
206 | 1 | public function newArtefact(array $columns, array $originalColumnNames = array()) |
|
227 | |||
228 | /** |
||
229 | * Export's the artefacts to CSV files and resets the array with the artefacts to free the memory. |
||
230 | * |
||
231 | * @param integer $timestamp The timestamp part of the original import file |
||
232 | * @param string $counter The counter part of the origin import file |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | 1 | public function export($timestamp, $counter) |
|
245 | |||
246 | /** |
||
247 | * Set's the exporter adapter instance. |
||
248 | * |
||
249 | * @param \TechDivision\Import\Adapter\ExportAdapterInterface $exportAdapter The exporter adapter instance |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | 2 | public function setExportAdapter(ExportAdapterInterface $exportAdapter) |
|
257 | |||
258 | /** |
||
259 | * Return's the exporter adapter instance. |
||
260 | * |
||
261 | * @return \TechDivision\Import\Adapter\ExportAdapterInterface The exporter adapter instance |
||
262 | */ |
||
263 | 2 | public function getExportAdapter() |
|
267 | |||
268 | /** |
||
269 | * Set's the ID of the product that has been created recently. |
||
270 | * |
||
271 | * @param string $lastEntityId The entity ID |
||
272 | * |
||
273 | * @return void |
||
274 | */ |
||
275 | 4 | public function setLastEntityId($lastEntityId) |
|
279 | |||
280 | /** |
||
281 | * Return's the ID of the product that has been created recently. |
||
282 | * |
||
283 | * @return string The entity Id |
||
284 | */ |
||
285 | 4 | public function getLastEntityId() |
|
289 | |||
290 | /** |
||
291 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
292 | * |
||
293 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
294 | */ |
||
295 | abstract public function isDebugMode(); |
||
296 | } |
||
297 |
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.