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 |
||
37 | class AttributeOptionObserver extends AbstractAttributeImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The attribute processor instance. |
||
42 | * |
||
43 | * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
||
44 | */ |
||
45 | protected $attributeBunchProcessor; |
||
46 | |||
47 | /** |
||
48 | * Initializes the observer with the passed subject instance. |
||
49 | * |
||
50 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject instance |
||
51 | * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
||
52 | */ |
||
53 | public function __construct( |
||
64 | |||
65 | /** |
||
66 | * Process the observer's business logic. |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | protected function process() |
||
84 | |||
85 | /** |
||
86 | * Prepare the attributes of the entity that has to be persisted. |
||
87 | * |
||
88 | * @return array The prepared attributes |
||
89 | */ |
||
90 | View Code Duplication | protected function prepareAttributes() |
|
108 | |||
109 | /** |
||
110 | * Initialize the attribute with the passed attributes and returns an instance. |
||
111 | * |
||
112 | * @param array $attr The attribute attributes |
||
113 | * |
||
114 | * @return array The initialized attribute |
||
115 | */ |
||
116 | protected function initializeAttribute(array $attr) |
||
120 | |||
121 | /** |
||
122 | * Return's the attribute bunch processor instance. |
||
123 | * |
||
124 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
125 | */ |
||
126 | protected function getAttributeBunchProcessor() |
||
130 | |||
131 | /** |
||
132 | * Set's the ID of the option that has been created recently. |
||
133 | * |
||
134 | * @param integer $lastOptionId The option ID |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | protected function setLastOptionId($lastOptionId) |
||
142 | |||
143 | /** |
||
144 | * Load's and return's the EAV attribute with the passed code. |
||
145 | * |
||
146 | * @param string $attributeCode The code of the EAV attribute to load |
||
147 | * |
||
148 | * @return array The EAV attribute |
||
149 | */ |
||
150 | protected function loadAttributeByAttributeCode($attributeCode) |
||
154 | |||
155 | /** |
||
156 | * Persist the passed attribute option. |
||
157 | * |
||
158 | * @param array $attributeOption The attribute option to persist |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | protected function persistAttributeOption(array $attributeOption) |
||
166 | } |
||
167 |
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.