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 |
||
| 41 | abstract class AbstractProductSubject extends AbstractSubject |
||
| 42 | { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The processor to read/write the necessary product data. |
||
| 46 | * |
||
| 47 | * @var \TechDivision\Import\Product\Services\ProductProcessorInterface |
||
| 48 | */ |
||
| 49 | protected $productProcessor; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The available stores. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $stores = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The available store websites. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $storeWebsites = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $attributes = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The available tax classes. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $taxClasses = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The available categories. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $categories = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The available root categories. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $rootCategories = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The ID of the product that has been created recently. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $lastEntityId; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The SKU of the product that has been created recently. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $lastSku; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The store view code the create the product/attributes for. |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $storeViewCode; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The default store. |
||
| 116 | * |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | protected $defaultStore; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The Magento 2 configuration. |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $coreConfigData; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The mapping for the SKUs to the created entity IDs. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $skuEntityIdMapping = array(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The mapping for the SKUs to the store view codes. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $skuStoreViewCodeMapping = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Initialize the subject instance. |
||
| 144 | * |
||
| 145 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger instance |
||
| 146 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration instance |
||
| 147 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 148 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 149 | */ |
||
| 150 | 3 | public function __construct( |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Set's the product processor instance. |
||
| 166 | * |
||
| 167 | * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | 3 | public function setProductProcessor(ProductProcessorInterface $productProcessor) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Return's the product processor instance. |
||
| 178 | * |
||
| 179 | * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance |
||
| 180 | */ |
||
| 181 | 3 | public function getProductProcessor() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Set's the SKU of the last imported product. |
||
| 188 | * |
||
| 189 | * @param string $lastSku The SKU |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function setLastSku($lastSku) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return's the SKU of the last imported product. |
||
| 200 | * |
||
| 201 | * @return string|null The SKU |
||
| 202 | */ |
||
| 203 | public function getLastSku() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set's the ID of the product that has been created recently. |
||
| 210 | * |
||
| 211 | * @param string $lastEntityId The entity ID |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 1 | public function setLastEntityId($lastEntityId) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return's the ID of the product that has been created recently. |
||
| 222 | * |
||
| 223 | * @return string The entity Id |
||
| 224 | */ |
||
| 225 | public function getLastEntityId() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Queries whether or not the SKU has already been processed. |
||
| 232 | * |
||
| 233 | * @param string $sku The SKU to check been processed |
||
| 234 | * |
||
| 235 | * @return boolean TRUE if the SKU has been processed, else FALSE |
||
| 236 | */ |
||
| 237 | public function hasBeenProcessed($sku) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Add the passed SKU => entity ID mapping. |
||
| 244 | * |
||
| 245 | * @param string $sku The SKU |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function addSkuEntityIdMapping($sku) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Add the passed SKU => store view code mapping. |
||
| 256 | * |
||
| 257 | * @param string $sku The SKU |
||
| 258 | * @param string $storeViewCode The store view code |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function addSkuStoreViewCodeMapping($sku, $storeViewCode) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set's the store view code the create the product/attributes for. |
||
| 269 | * |
||
| 270 | * @param string $storeViewCode The store view code |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | 3 | public function setStoreViewCode($storeViewCode) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Return's the store view code the create the product/attributes for. |
||
| 281 | * |
||
| 282 | * @param string|null $default The default value to return, if the store view code has not been set |
||
| 283 | * |
||
| 284 | * @return string The store view code |
||
| 285 | */ |
||
| 286 | public function getStoreViewCode($default = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return's the default store. |
||
| 303 | * |
||
| 304 | * @return array The default store |
||
| 305 | */ |
||
| 306 | public function getDefaultStore() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 316 | */ |
||
| 317 | public function setUp() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Clean up the global data after importing the bunch. |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | public function tearDown() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Return's the store ID of the actual row, or of the default store |
||
| 369 | * if no store view code is set in the CSV file. |
||
| 370 | * |
||
| 371 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 372 | * |
||
| 373 | * @return integer The ID of the actual store |
||
| 374 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 375 | */ |
||
| 376 | public function getRowStoreId($default = null) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return's the tax class ID for the passed tax class name. |
||
| 406 | * |
||
| 407 | * @param string $taxClassName The tax class name to return the ID for |
||
| 408 | * |
||
| 409 | * @return integer The tax class ID |
||
| 410 | * @throws \Exception Is thrown, if the tax class with the requested name is not available |
||
| 411 | */ |
||
| 412 | View Code Duplication | public function getTaxClassIdByTaxClassName($taxClassName) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Return's the store website for the passed code. |
||
| 433 | * |
||
| 434 | * @param string $code The code of the store website to return the ID for |
||
| 435 | * |
||
| 436 | * @return integer The store website ID |
||
| 437 | * @throws \Exception Is thrown, if the store website with the requested code is not available |
||
| 438 | */ |
||
| 439 | View Code Duplication | public function getStoreWebsiteIdByCode($code) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Return's the category with the passed path. |
||
| 460 | * |
||
| 461 | * @param string $path The path of the category to return |
||
| 462 | * |
||
| 463 | * @return array The category |
||
| 464 | * @throws \Exception Is thrown, if the requested category is not available |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function getCategoryByPath($path) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Return's the category with the passed ID. |
||
| 487 | * |
||
| 488 | * @param integer $categoryId The ID of the category to return |
||
| 489 | * |
||
| 490 | * @return array The category data |
||
| 491 | * @throws \Exception Is thrown, if the category is not available |
||
| 492 | */ |
||
| 493 | public function getCategory($categoryId) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Return's the root category for the actual view store. |
||
| 516 | * |
||
| 517 | * @return array The store's root category |
||
| 518 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 519 | */ |
||
| 520 | public function getRootCategory() |
||
| 537 | } |
||
| 538 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: