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 | abstract class AbstractMultiselectCallback extends AbstractCallback |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The EAV aware processor. |
||
| 43 | * |
||
| 44 | * @var \TechDivision\Import\Services\EavAwareProcessorInterface |
||
| 45 | */ |
||
| 46 | protected $eavAwareProcessor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Initialize the callback with the passed processor instance. |
||
| 50 | * |
||
| 51 | * @param \TechDivision\Import\Services\EavAwareProcessorInterface $eavAwareProcessor The processor instance |
||
| 52 | */ |
||
| 53 | public function __construct(EavAwareProcessorInterface $eavAwareProcessor) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Will be invoked by a observer it has been registered for. |
||
| 60 | * |
||
| 61 | * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer |
||
| 62 | * |
||
| 63 | * @return mixed The modified value |
||
| 64 | */ |
||
| 65 | public function handle(AttributeCodeAndValueAwareObserverInterface $observer) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return's the EAV aware processor instance. |
||
| 146 | * |
||
| 147 | * @return \TechDivision\Import\Services\EavAwareProcessorInterface The processor instance |
||
| 148 | */ |
||
| 149 | protected function getEavAwareProcessor() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return's the store ID of the actual row, or of the default store |
||
| 156 | * if no store view code is set in the CSV file. |
||
| 157 | * |
||
| 158 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 159 | * |
||
| 160 | * @return integer The ID of the actual store |
||
| 161 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 162 | */ |
||
| 163 | protected function getRowStoreId($default = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Load's and return's the EAV attribute option value with the passed code, store ID and value. |
||
| 170 | * |
||
| 171 | * @param string $attributeCode The code of the EAV attribute option to load |
||
| 172 | * @param integer $storeId The store ID of the attribute option to load |
||
| 173 | * @param string $value The value of the attribute option to load |
||
| 174 | * |
||
| 175 | * @return array The EAV attribute option value |
||
| 176 | */ |
||
| 177 | protected function loadEavAttributeOptionValueByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value) |
||
| 181 | } |
||
| 182 |
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.