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 |
||
39 | class OptionSubject extends AbstractAttributeSubject implements OptionSubjectInterface, FileUploadSubjectInterface, CastValueSubjectInterface |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The trait that provides file upload functionality. |
||
44 | * |
||
45 | * @var \TechDivision\Import\Subjects\FileUploadTrait |
||
46 | */ |
||
47 | use FileUploadTrait; |
||
48 | |||
49 | /** |
||
50 | * The ID of the option that has been created recently. |
||
51 | * |
||
52 | * @var integer |
||
53 | */ |
||
54 | protected $lastOptionId; |
||
55 | |||
56 | /** |
||
57 | * The value => option ID mapping. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $attributeCodeValueOptionIdMapping = array(); |
||
62 | |||
63 | /** |
||
64 | * Initializes the previously loaded global data for exactly one bunch. |
||
65 | * |
||
66 | * @param string $serial The serial of the actual import |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function setUp($serial) |
||
99 | |||
100 | /** |
||
101 | * Map's the passed attribue code and value to the option ID that has been created recently. |
||
102 | * |
||
103 | * @param string $attributeCode The attriburte code that has to be mapped |
||
104 | * @param string $value The value that has to be mapped |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function addAddtributeCodeValueOptionIdMapping($attributeCode, $value) |
||
112 | |||
113 | /** |
||
114 | * Queries whether or not the attribute with the passed code/value has already been processed. |
||
115 | * |
||
116 | * @param string $attributeCode The attribute code to check |
||
117 | * @param string $value The option value to check |
||
118 | * |
||
119 | * @return boolean TRUE if the path has been processed, else FALSE |
||
120 | */ |
||
121 | public function hasBeenProcessed($attributeCode, $value) |
||
125 | |||
126 | /** |
||
127 | * Return's the ID of the attribute that has been created recently. |
||
128 | * |
||
129 | * @return integer The attribute ID |
||
130 | */ |
||
131 | public function getLastEntityId() |
||
135 | |||
136 | /** |
||
137 | * Set's the ID of the option that has been created recently. |
||
138 | * |
||
139 | * @param integer $lastOptionId The option ID |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function setLastOptionId($lastOptionId) |
||
147 | |||
148 | /** |
||
149 | * Return's the ID of the option that has been created recently. |
||
150 | * |
||
151 | * @return integer The option ID |
||
152 | */ |
||
153 | public function getLastOptionId() |
||
157 | |||
158 | /** |
||
159 | * Pre-load the option ID for the passed EAV attribute option. |
||
160 | * |
||
161 | * @param array $attributeOption The EAV attribute option with the ID that has to be pre-loaded |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function preLoadOptionId(array $attributeOption) |
||
169 | |||
170 | /** |
||
171 | * Cast's the passed value based on the backend type information. |
||
172 | * |
||
173 | * @param string $backendType The backend type to cast to |
||
174 | * @param mixed $value The value to be casted |
||
175 | * |
||
176 | * @return mixed The casted value |
||
177 | */ |
||
178 | public function castValueByBackendType($backendType, $value) |
||
203 | } |
||
204 |
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.