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 |
||
21 | abstract class AbstractProductCommand extends Command |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Product visibility value |
||
26 | */ |
||
27 | const CATALOG_SEARCH_VISIBILITY = 4; |
||
28 | |||
29 | /** |
||
30 | * @var ProductFactory |
||
31 | */ |
||
32 | private $_productFactory; |
||
33 | |||
34 | /** |
||
35 | * @var Client |
||
36 | */ |
||
37 | private $_eventClient; |
||
38 | |||
39 | /** |
||
40 | * @var Export |
||
41 | */ |
||
42 | private $_export; |
||
43 | |||
44 | /** |
||
45 | * @var ExportFactory |
||
46 | */ |
||
47 | private $_exportFactory; |
||
48 | |||
49 | /** |
||
50 | * @var PersonalisedProductsLogger |
||
51 | */ |
||
52 | private $_logger; |
||
53 | |||
54 | /** |
||
55 | * AbstractProductCommand constructor. |
||
56 | * @param ProductFactory $productFactory |
||
57 | * @param Client $eventClient |
||
58 | * @param AppState $appState |
||
59 | */ |
||
60 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * Send product data to PredictionIO |
||
83 | * |
||
84 | * @param $collection |
||
85 | * @return int |
||
86 | */ |
||
87 | protected function _sendProductData($collection) |
||
110 | |||
111 | /** |
||
112 | * Get product collection |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function _getProductCollection() |
||
124 | |||
125 | /** |
||
126 | * Get category collection for each product |
||
127 | * |
||
128 | * @param $productId |
||
129 | * @return array |
||
130 | */ |
||
131 | private function _getProductCategoryCollection($productId) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Send the new product to PredictionIO |
||
142 | * |
||
143 | * @param $productId |
||
144 | * @return bool |
||
145 | */ |
||
146 | private function _sendToPredictionIO($productId) |
||
153 | |||
154 | /** |
||
155 | * Mark product as exported in DB |
||
156 | * |
||
157 | * @param $exportId |
||
158 | */ |
||
159 | View Code Duplication | private function _setProductExported($exportId) |
|
169 | |||
170 | } |
||
171 |
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.