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 |
||
18 | class Crosssell |
||
19 | { |
||
20 | /** |
||
21 | * @var Complementary |
||
22 | */ |
||
23 | private $_complementaryEngine; |
||
24 | |||
25 | /** |
||
26 | * @var CustomerSession |
||
27 | */ |
||
28 | private $_customerSession; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $_basketProducts; |
||
34 | |||
35 | /** |
||
36 | * @var ProductFactory |
||
37 | */ |
||
38 | private $_productFactory; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $_products; |
||
44 | |||
45 | /** |
||
46 | * @var Session |
||
47 | */ |
||
48 | protected $_checkoutSession; |
||
49 | |||
50 | /** |
||
51 | * Crosssell constructor. |
||
52 | * @param Complementary $complementaryEngine |
||
53 | * @param CustomerSession $customerSession |
||
54 | * @param ProductFactory $productFactory |
||
55 | * @param Session $session |
||
56 | */ |
||
57 | public function __construct( |
||
68 | |||
69 | /** |
||
70 | * Query the PredictionIO engine for product data |
||
71 | * |
||
72 | * @return array|bool |
||
73 | */ |
||
74 | public function getProductCollection() |
||
85 | |||
86 | /** |
||
87 | * Loop over each of the rules in the returned data from PredictionIO |
||
88 | * |
||
89 | * @param array $items |
||
90 | * @return array |
||
91 | */ |
||
92 | private function _getPredictedProducts(array $items) |
||
101 | |||
102 | /** |
||
103 | * Build product ID collection array from PredictionIO engine data |
||
104 | * |
||
105 | * @param array $items |
||
106 | * @param $productIds |
||
107 | * @return $this |
||
108 | */ |
||
109 | private function _getProductIds(array $items, &$productIds) |
||
119 | |||
120 | /** |
||
121 | * Get a new product collection from prediction IO result set |
||
122 | * |
||
123 | * @param array $personalisedIds |
||
124 | * @return $this |
||
125 | */ |
||
126 | View Code Duplication | public function getPersonalisedProductCollection(array $personalisedIds) |
|
134 | |||
135 | /** |
||
136 | * Get all product ids in the cart |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | private function _getCartProductIds() |
||
153 | |||
154 | /** |
||
155 | * Get the quote from the session |
||
156 | * |
||
157 | * @return \Magento\Quote\Model\Quote |
||
158 | */ |
||
159 | public function getQuote() |
||
163 | } |
||
164 |
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.