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 |
||
38 | class BunchSubject extends AbstractCategorySubject implements ExportableSubjectInterface |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The trait that implements the export functionality. |
||
43 | * |
||
44 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
45 | */ |
||
46 | use ExportableTrait; |
||
47 | |||
48 | /** |
||
49 | * The mapping for the supported backend types (for the category entity) => persist methods. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $backendTypes = array( |
||
54 | 'datetime' => array('persistCategoryDatetimeAttribute', 'loadCategoryDatetimeAttribute'), |
||
55 | 'decimal' => array('persistCategoryDecimalAttribute', 'loadCategoryDecimalAttribute'), |
||
56 | 'int' => array('persistCategoryIntAttribute', 'loadCategoryIntAttribute'), |
||
57 | 'text' => array('persistCategoryTextAttribute', 'loadCategoryTextAttribute'), |
||
58 | 'varchar' => array('persistCategoryVarcharAttribute', 'loadCategoryVarcharAttribute'), |
||
59 | 'image' => array('persistCategoryVarcharAttribute', 'loadCategoryVarcharAttribute') |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * The array with the available display mode keys. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $availableDisplayModes = array( |
||
68 | 'Products only' => DisplayModeKeys::DISPLAY_MODE_PRODUCTS_ONLY, |
||
69 | 'Static block only' => DisplayModeKeys::DISPLAY_MODE_STATIC_BLOCK_ONLY, |
||
70 | 'Static block and products' => DisplayModeKeys::DISPLAY_MODE_BOTH |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * The array with the available page layout keys. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $availablePageLayouts = array( |
||
79 | '1 column' => PageLayoutKeys::PAGE_LAYOUT_1_COLUMN, |
||
80 | '2 columns with left bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_LEFT, |
||
81 | '2 columns with right bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_RIGHT, |
||
82 | '3 columns' => PageLayoutKeys::PAGE_LAYOUT_3_COLUMNS, |
||
83 | 'Empty' => PageLayoutKeys::PAGE_LAYOUT_EMPTY |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * The default callback mappings for the Magento standard category attributes. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $defaultCallbackMappings = array( |
||
92 | 'display_mode' => array('TechDivision\\Import\\Category\\Callbacks\\DisplayModeCallback'), |
||
93 | 'page_layout' => array('TechDivision\\Import\\Category\\Callbacks\\PageLayoutCallback'), |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
98 | * |
||
99 | * @param integer $isUserDefined The flag itself |
||
100 | * |
||
101 | * @return array The array with the EAV attributes matching the passed flag |
||
102 | */ |
||
103 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
107 | |||
108 | /** |
||
109 | * Intializes the previously loaded global data for exactly one bunch. |
||
110 | * |
||
111 | * @return void |
||
112 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
113 | */ |
||
114 | public function setUp() |
||
156 | |||
157 | /** |
||
158 | * Return's the display mode for the passed display mode string. |
||
159 | * |
||
160 | * @param string $displayMode The display mode string to return the key for |
||
161 | * |
||
162 | * @return integer The requested display mode |
||
163 | * @throws \Exception Is thrown, if the requested display mode is not available |
||
164 | */ |
||
165 | View Code Duplication | public function getDisplayModeByValue($displayMode) |
|
183 | |||
184 | /** |
||
185 | * Return's the page layout for the passed page layout string. |
||
186 | * |
||
187 | * @param string $pageLayout The page layout string to return the key for |
||
188 | * |
||
189 | * @return integer The requested page layout |
||
190 | * @throws \Exception Is thrown, if the requested page layout is not available |
||
191 | */ |
||
192 | View Code Duplication | public function getPageLayoutByValue($pageLayout) |
|
210 | |||
211 | /** |
||
212 | * Cast's the passed value based on the backend type information. |
||
213 | * |
||
214 | * @param string $backendType The backend type to cast to |
||
215 | * @param mixed $value The value to be casted |
||
216 | * |
||
217 | * @return mixed The casted value |
||
218 | */ |
||
219 | public function castValueByBackendType($backendType, $value) |
||
240 | |||
241 | /** |
||
242 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
243 | * |
||
244 | * @return array The mapping for the supported backend types |
||
245 | */ |
||
246 | public function getBackendTypes() |
||
250 | |||
251 | /** |
||
252 | * Return's the category with the passed ID. |
||
253 | * |
||
254 | * @param string $id The ID of the category to return |
||
255 | * |
||
256 | * @return array The category |
||
257 | */ |
||
258 | public function loadCategory($id) |
||
262 | |||
263 | /** |
||
264 | * Load's and return's the datetime attribute with the passed entity/attribute/store ID. |
||
265 | * |
||
266 | * @param integer $entityId The entity ID of the attribute |
||
267 | * @param integer $attributeId The attribute ID of the attribute |
||
268 | * @param integer $storeId The store ID of the attribute |
||
269 | * |
||
270 | * @return array|null The datetime attribute |
||
271 | */ |
||
272 | public function loadCategoryDatetimeAttribute($entityId, $attributeId, $storeId) |
||
276 | |||
277 | /** |
||
278 | * Load's and return's the decimal attribute with the passed entity/attribute/store ID. |
||
279 | * |
||
280 | * @param integer $entityId The entity ID of the attribute |
||
281 | * @param integer $attributeId The attribute ID of the attribute |
||
282 | * @param integer $storeId The store ID of the attribute |
||
283 | * |
||
284 | * @return array|null The decimal attribute |
||
285 | */ |
||
286 | public function loadCategoryDecimalAttribute($entityId, $attributeId, $storeId) |
||
290 | |||
291 | /** |
||
292 | * Load's and return's the integer attribute with the passed entity/attribute/store ID. |
||
293 | * |
||
294 | * @param integer $entityId The entity ID of the attribute |
||
295 | * @param integer $attributeId The attribute ID of the attribute |
||
296 | * @param integer $storeId The store ID of the attribute |
||
297 | * |
||
298 | * @return array|null The integer attribute |
||
299 | */ |
||
300 | public function loadCategoryIntAttribute($entityId, $attributeId, $storeId) |
||
304 | |||
305 | /** |
||
306 | * Load's and return's the text attribute with the passed entity/attribute/store ID. |
||
307 | * |
||
308 | * @param integer $entityId The entity ID of the attribute |
||
309 | * @param integer $attributeId The attribute ID of the attribute |
||
310 | * @param integer $storeId The store ID of the attribute |
||
311 | * |
||
312 | * @return array|null The text attribute |
||
313 | */ |
||
314 | public function loadCategoryTextAttribute($entityId, $attributeId, $storeId) |
||
318 | |||
319 | /** |
||
320 | * Load's and return's the varchar attribute with the passed entity/attribute/store ID. |
||
321 | * |
||
322 | * @param integer $entityId The entity ID of the attribute |
||
323 | * @param integer $attributeId The attribute ID of the attribute |
||
324 | * @param integer $storeId The store ID of the attribute |
||
325 | * |
||
326 | * @return array|null The varchar attribute |
||
327 | */ |
||
328 | public function loadCategoryVarcharAttribute($entityId, $attributeId, $storeId) |
||
332 | |||
333 | /** |
||
334 | * Persist's the passed category data and return's the ID. |
||
335 | * |
||
336 | * @param array $category The category data to persist |
||
337 | * |
||
338 | * @return string The ID of the persisted entity |
||
339 | */ |
||
340 | public function persistCategory($category) |
||
344 | |||
345 | /** |
||
346 | * Persist's the passed category varchar attribute. |
||
347 | * |
||
348 | * @param array $attribute The attribute to persist |
||
349 | * |
||
350 | * @return void |
||
351 | */ |
||
352 | public function persistCategoryVarcharAttribute($attribute) |
||
356 | |||
357 | /** |
||
358 | * Persist's the passed category integer attribute. |
||
359 | * |
||
360 | * @param array $attribute The attribute to persist |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | public function persistCategoryIntAttribute($attribute) |
||
368 | |||
369 | /** |
||
370 | * Persist's the passed category decimal attribute. |
||
371 | * |
||
372 | * @param array $attribute The attribute to persist |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | public function persistCategoryDecimalAttribute($attribute) |
||
380 | |||
381 | /** |
||
382 | * Persist's the passed category datetime attribute. |
||
383 | * |
||
384 | * @param array $attribute The attribute to persist |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | public function persistCategoryDatetimeAttribute($attribute) |
||
392 | |||
393 | /** |
||
394 | * Persist's the passed category text attribute. |
||
395 | * |
||
396 | * @param array $attribute The attribute to persist |
||
397 | * |
||
398 | * @return void |
||
399 | */ |
||
400 | public function persistCategoryTextAttribute($attribute) |
||
404 | |||
405 | /** |
||
406 | * Persist's the URL rewrite with the passed data. |
||
407 | * |
||
408 | * @param array $row The URL rewrite to persist |
||
409 | * |
||
410 | * @return string The ID of the persisted entity |
||
411 | */ |
||
412 | public function persistUrlRewrite($row) |
||
416 | |||
417 | /** |
||
418 | * Delete's the URL rewrite(s) with the passed attributes. |
||
419 | * |
||
420 | * @param array $row The attributes of the entity to delete |
||
421 | * @param string|null $name The name of the prepared statement that has to be executed |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | public function deleteUrlRewrite($row, $name = null) |
||
429 | |||
430 | /** |
||
431 | * Delete's the entity with the passed attributes. |
||
432 | * |
||
433 | * @param array $row The attributes of the entity to delete |
||
434 | * @param string|null $name The name of the prepared statement that has to be executed |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | public function deleteCategory($row, $name = null) |
||
442 | } |
||
443 |
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: