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 |
||
14 | final class PutOptionBasedConfigurableItemToCartHandler |
||
15 | { |
||
16 | /** |
||
17 | * @var OrderRepositoryInterface |
||
18 | */ |
||
19 | private $cartRepository; |
||
20 | |||
21 | /** |
||
22 | * @var ProductRepositoryInterface |
||
23 | */ |
||
24 | private $productRepository; |
||
25 | |||
26 | /** |
||
27 | * @var OrderModifierInterface |
||
28 | */ |
||
29 | private $orderModifier; |
||
30 | |||
31 | public function __construct( |
||
40 | |||
41 | View Code Duplication | public function handle(PutOptionBasedConfigurableItemToCart $putConfigurableItemToCart) |
|
57 | |||
58 | /** |
||
59 | * @param array $options |
||
60 | * @param ProductInterface $product |
||
61 | * |
||
62 | * @return null|ProductVariantInterface |
||
63 | */ |
||
64 | private function getVariant(array $options, ProductInterface $product) |
||
74 | |||
75 | /** |
||
76 | * @param array $options |
||
77 | * @param ProductVariantInterface $variant |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | private function areOptionsMatched(array $options, ProductVariantInterface $variant) |
||
91 | } |
||
92 |
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.