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 Cron |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $_exportedProducts = []; |
||
24 | |||
25 | /** |
||
26 | * @var Client |
||
27 | */ |
||
28 | private $_eventClient; |
||
29 | |||
30 | /** |
||
31 | * @var ExportCollection |
||
32 | */ |
||
33 | private $_exportCollection; |
||
34 | |||
35 | /** |
||
36 | * @var CollectionFactory |
||
37 | */ |
||
38 | protected $_ecFactory; |
||
39 | |||
40 | /** |
||
41 | * @var \Richdynamix\PersonalisedProducts\Model\ExportFactory |
||
42 | */ |
||
43 | private $_exportFactory; |
||
44 | |||
45 | /** |
||
46 | * @var PersonalisedProductsLogger |
||
47 | */ |
||
48 | private $_logger; |
||
49 | |||
50 | /** |
||
51 | * Cron constructor. |
||
52 | * @param Client $client |
||
53 | * @param ExportCollection $exportCollection |
||
54 | * @param CollectionFactory $ecFactory |
||
55 | * @param PersonalisedProductsLogger $logger |
||
56 | */ |
||
57 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * Method called on the cron to do the export |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function export() |
||
98 | |||
99 | /** |
||
100 | * Get product and category collections for export |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | private function _getProductsForExport() |
||
122 | |||
123 | /** |
||
124 | * Set a list of product successfully exported |
||
125 | * |
||
126 | * @param $productId |
||
127 | */ |
||
128 | private function _setExportedProducts($productId) |
||
132 | |||
133 | /** |
||
134 | * Getter for the exportedProducts to be updated |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | private function _getExportedProducts() |
||
142 | |||
143 | /** |
||
144 | * Update the database row with the `is_exported` as 1 |
||
145 | */ |
||
146 | View Code Duplication | private function _updateDatabase() |
|
159 | |||
160 | /** |
||
161 | * Load the item model by using product ID as identifier. |
||
162 | * |
||
163 | * //todo move this into a proper loadByProductId method on the model. |
||
164 | * |
||
165 | * @param $productId |
||
166 | * @return $this |
||
167 | */ |
||
168 | private function _getItemModel($productId) |
||
175 | |||
176 | } |
||
177 |
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.