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 |
||
40 | class AttributeOptionObserver extends AbstractAttributeImportObserver implements DynamicAttributeObserverInterface |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * The attribute processor instance. |
||
45 | * |
||
46 | * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
||
47 | */ |
||
48 | protected $attributeBunchProcessor; |
||
49 | |||
50 | /** |
||
51 | * The attribute loader instance. |
||
52 | * |
||
53 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
54 | */ |
||
55 | protected $attributeLoader; |
||
56 | |||
57 | /** |
||
58 | * Initialize the dedicated column. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $columns = array(MemberNames::SORT_ORDER => array(ColumnKeys::SORT_ORDER, BackendTypeKeys::BACKEND_TYPE_INT)); |
||
63 | |||
64 | /** |
||
65 | * Initializes the observer with the passed subject instance. |
||
66 | * |
||
67 | * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
||
68 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface $attributeLoader The attribute loader instance |
||
69 | */ |
||
70 | public function __construct( |
||
77 | |||
78 | /** |
||
79 | * Process the observer's business logic. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | protected function process() |
||
100 | |||
101 | /** |
||
102 | * Appends the dynamic to the static attributes for the EAV attribute |
||
103 | * and returns them. |
||
104 | * |
||
105 | * @return array The array with all available attributes |
||
106 | */ |
||
107 | protected function prepareDynamicAttributes() |
||
111 | |||
112 | /** |
||
113 | * Prepare the attributes of the entity that has to be persisted. |
||
114 | * |
||
115 | * @return array The prepared attributes |
||
116 | */ |
||
117 | View Code Duplication | protected function prepareAttributes() |
|
131 | |||
132 | /** |
||
133 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
134 | * |
||
135 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
136 | * |
||
137 | * @return array The initialized entity |
||
138 | */ |
||
139 | protected function loadRawEntity(array $data = array()) |
||
143 | |||
144 | /** |
||
145 | * Initialize the EAV attribute option with the passed attributes and returns an instance. |
||
146 | * |
||
147 | * @param array $attr The EAV attribute option attributes |
||
148 | * |
||
149 | * @return array The initialized EAV attribute option |
||
150 | */ |
||
151 | protected function initializeAttribute(array $attr) |
||
155 | |||
156 | /** |
||
157 | * Return's the attribute bunch processor instance. |
||
158 | * |
||
159 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
160 | */ |
||
161 | protected function getAttributeBunchProcessor() |
||
165 | |||
166 | /** |
||
167 | * Queries whether or not the attribute with the passed code/value has already been processed. |
||
168 | * |
||
169 | * @param string $attributeCode The attribute code to check |
||
170 | * @param string $value The option value to check |
||
171 | * |
||
172 | * @return boolean TRUE if the path has been processed, else FALSE |
||
173 | */ |
||
174 | protected function hasBeenProcessed($attributeCode, $value) |
||
178 | |||
179 | /** |
||
180 | * Set's the ID of the option that has been created recently. |
||
181 | * |
||
182 | * @param integer $lastOptionId The option ID |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | protected function setLastOptionId($lastOptionId) |
||
190 | |||
191 | /** |
||
192 | * Return's the EAV attribute with the passed entity type ID and code. |
||
193 | * |
||
194 | * @param integer $entityTypeId The entity type ID of the EAV attribute to return |
||
195 | * @param string $attributeCode The code of the EAV attribute to return |
||
196 | * |
||
197 | * @return array The EAV attribute |
||
198 | */ |
||
199 | public function loadAttributeByEntityTypeIdAndAttributeCode($entityTypeId, $attributeCode) |
||
203 | |||
204 | /** |
||
205 | * Persist the passed attribute option. |
||
206 | * |
||
207 | * @param array $attributeOption The attribute option to persist |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | protected function persistAttributeOption(array $attributeOption) |
||
215 | } |
||
216 |
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.