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 AttributeOptionValueObserver extends AbstractAttributeImportObserver |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The attribute processor instance. |
||
43 | * |
||
44 | * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
||
45 | */ |
||
46 | protected $attributeBunchProcessor; |
||
47 | |||
48 | /** |
||
49 | * Initializes the observer with the passed subject instance. |
||
50 | * |
||
51 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject instance |
||
52 | * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
||
53 | */ |
||
54 | public function __construct( |
||
65 | |||
66 | /** |
||
67 | * Process the observer's business logic. |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | protected function process() |
||
80 | |||
81 | /** |
||
82 | * Prepare the attributes of the entity that has to be persisted. |
||
83 | * |
||
84 | * @return array The prepared attributes |
||
85 | */ |
||
86 | View Code Duplication | protected function prepareAttributes() |
|
107 | |||
108 | /** |
||
109 | * Initialize the attribute with the passed attributes and returns an instance. |
||
110 | * |
||
111 | * @param array $attr The attribute attributes |
||
112 | * |
||
113 | * @return array The initialized attribute |
||
114 | */ |
||
115 | protected function initializeAttribute(array $attr) |
||
119 | |||
120 | /** |
||
121 | * Return's the attribute bunch processor instance. |
||
122 | * |
||
123 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
124 | */ |
||
125 | protected function getAttributeBunchProcessor() |
||
129 | |||
130 | /** |
||
131 | * Return's the ID of the option that has been created recently. |
||
132 | * |
||
133 | * @return integer The option ID |
||
134 | */ |
||
135 | protected function getLastOptionId() |
||
139 | |||
140 | /** |
||
141 | * Persist the passed attribute option value. |
||
142 | * |
||
143 | * @param array $attributeOptionValue The attribute option value to persist |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | protected function persistAttributeOptionValue(array $attributeOptionValue) |
||
151 | } |
||
152 |
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.