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 |
||
24 | class Upsell extends \Magento\Catalog\Block\Product\ProductList\Upsell |
||
25 | { |
||
26 | /** |
||
27 | * @var Config |
||
28 | */ |
||
29 | private $_config; |
||
30 | |||
31 | /** |
||
32 | * @var ProductFactory |
||
33 | */ |
||
34 | private $_productFactory; |
||
35 | |||
36 | /** |
||
37 | * @var PersonalisedUpsell |
||
38 | */ |
||
39 | private $_upsell; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $_isPredictedResults = false; |
||
45 | |||
46 | /** |
||
47 | * Upsell constructor. |
||
48 | * @param Context $context |
||
49 | * @param Cart $checkoutCart |
||
50 | * @param Visibility $productVisibility |
||
51 | * @param Session $checkoutSession |
||
52 | * @param Manager $moduleManager |
||
53 | * @param ProductFactory $productFactory |
||
54 | * @param Config $config |
||
55 | * @param PersonalisedUpsell $upsell |
||
56 | * @param CustomerSession $customerSession |
||
57 | * @param array $data |
||
58 | */ |
||
59 | View Code Duplication | public function __construct( |
|
84 | |||
85 | /** |
||
86 | * Rewrite parent _prepareData method to use PredictionIO results when available |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | protected function _prepareData() |
||
126 | |||
127 | /** |
||
128 | * Get the URL for product upsells block via ajax |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getUpsellAjaxUrl() |
||
136 | |||
137 | /** |
||
138 | * Check if the displayed results are from PredictionIO |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isPredictedResults() |
||
146 | } |
||
147 |
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.