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 |
||
7 | View Code Duplication | class ModelChild extends MarketModel |
|
|
|||
8 | { |
||
9 | protected $offersCount = null; |
||
10 | |||
11 | protected $rating = null; |
||
12 | |||
13 | protected $reviewsCount = null; |
||
14 | |||
15 | protected $articlesCount = null; |
||
16 | |||
17 | protected $isNew = null; |
||
18 | |||
19 | protected $vendorId = null; |
||
20 | |||
21 | protected $gradesCount = null; |
||
22 | |||
23 | protected $categoryId = null; |
||
24 | |||
25 | protected $id = null; |
||
26 | |||
27 | protected $photos = null; |
||
28 | |||
29 | protected $link = null; |
||
30 | |||
31 | protected $isGroup = null; |
||
32 | |||
33 | protected $vendorName = null; |
||
34 | |||
35 | protected $name = null; |
||
36 | |||
37 | protected $prices = null; |
||
38 | |||
39 | protected $description = null; |
||
40 | |||
41 | protected $facts = null; |
||
42 | |||
43 | protected $mainPhoto = null; |
||
44 | |||
45 | protected $parent = null; |
||
46 | |||
47 | /** |
||
48 | * Additional properties available during listed requests. |
||
49 | */ |
||
50 | protected $childrenCount = null; |
||
51 | |||
52 | protected $popularity = null; |
||
53 | |||
54 | protected $mappingClasses = [ |
||
55 | 'photos' => 'Yandex\Market\Content\Models\Photos', |
||
56 | 'prices' => 'Yandex\Market\Content\Models\Prices', |
||
57 | 'facts' => 'Yandex\Market\Content\Models\Facts', |
||
58 | 'mainPhoto' => 'Yandex\Market\Content\Models\Base\Photo', |
||
59 | 'parent' => 'Yandex\Market\Content\Models\ModelParent' |
||
60 | ]; |
||
61 | |||
62 | protected $propNameMap = [ |
||
63 | 'vendor' => 'vendorName', |
||
64 | 'parentModel' => 'parent', |
||
65 | 'modificationsCount' => 'childrenCount', |
||
66 | 'gradeCount' => 'gradesCount' |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * Constructor |
||
71 | * |
||
72 | * @param array $data |
||
73 | */ |
||
74 | 1 | public function __construct($data = array()) |
|
84 | |||
85 | /** |
||
86 | * Retrieve the offersCount property |
||
87 | * |
||
88 | * @return int|null |
||
89 | */ |
||
90 | 1 | public function getOffersCount() |
|
94 | |||
95 | /** |
||
96 | * Retrieve the rating property |
||
97 | * |
||
98 | * @return float|null |
||
99 | */ |
||
100 | 1 | public function getRating() |
|
104 | |||
105 | /** |
||
106 | * Retrieve the reviewsCount property |
||
107 | * |
||
108 | * @return int|null |
||
109 | */ |
||
110 | 1 | public function getReviewsCount() |
|
114 | |||
115 | /** |
||
116 | * Retrieve the articlesCount property |
||
117 | * |
||
118 | * @return int|null |
||
119 | */ |
||
120 | 1 | public function getArticlesCount() |
|
124 | |||
125 | /** |
||
126 | * Retrieve the isNew property |
||
127 | * |
||
128 | * @return bool|null |
||
129 | */ |
||
130 | 1 | public function getIsNew() |
|
142 | |||
143 | /** |
||
144 | * Retrieve the vendorId property |
||
145 | * |
||
146 | * @return int|null |
||
147 | */ |
||
148 | 1 | public function getVendorId() |
|
152 | |||
153 | /** |
||
154 | * Retrieve the gradesCount property |
||
155 | * |
||
156 | * @return int|null |
||
157 | */ |
||
158 | 1 | public function getGradesCount() |
|
162 | |||
163 | /** |
||
164 | * Retrieve the categoryId property |
||
165 | * |
||
166 | * @return int|null |
||
167 | */ |
||
168 | 1 | public function getCategoryId() |
|
172 | |||
173 | /** |
||
174 | * Retrieve the id property |
||
175 | * |
||
176 | * @return int|null |
||
177 | */ |
||
178 | 1 | public function getId() |
|
182 | |||
183 | /** |
||
184 | * Retrieve the id property |
||
185 | * |
||
186 | * @return Photos|null |
||
187 | */ |
||
188 | 1 | public function getPhotos() |
|
192 | |||
193 | /** |
||
194 | * Retrieve the link property |
||
195 | * |
||
196 | * @return string|null |
||
197 | */ |
||
198 | 1 | public function getLink() |
|
202 | |||
203 | /** |
||
204 | * Retrieve the isGroup property |
||
205 | * |
||
206 | * @return bool|null |
||
207 | */ |
||
208 | 1 | public function getIsGroup() |
|
212 | |||
213 | /** |
||
214 | * Retrieve the vendorName property |
||
215 | * @return string|null |
||
216 | */ |
||
217 | 1 | public function getVendorName() |
|
221 | |||
222 | /** |
||
223 | * Retrieve the name property |
||
224 | * @return string|null |
||
225 | */ |
||
226 | 1 | public function getName() |
|
230 | |||
231 | /** |
||
232 | * Retrieve the prices property |
||
233 | * |
||
234 | * @return Prices|null |
||
235 | */ |
||
236 | 1 | public function getPrices() |
|
240 | |||
241 | /** |
||
242 | * Retrieve the description property |
||
243 | * @return string|null |
||
244 | */ |
||
245 | 1 | public function getDescription() |
|
249 | |||
250 | /** |
||
251 | * Retrieve the facts property |
||
252 | * |
||
253 | * @return Facts|null |
||
254 | */ |
||
255 | public function getFacts() |
||
259 | |||
260 | /** |
||
261 | * Retrieve the mainPhoto property |
||
262 | * |
||
263 | * @return Photo|null |
||
264 | */ |
||
265 | 1 | public function getMainPhoto() |
|
269 | |||
270 | /** |
||
271 | * Retrieve the parent property |
||
272 | * |
||
273 | * @return ModelParent|null |
||
274 | */ |
||
275 | 1 | public function getParent() |
|
279 | |||
280 | /** |
||
281 | * Retrieve the childrenCount property |
||
282 | * |
||
283 | * @note Property available during /category/{category_id}/models request. |
||
284 | * |
||
285 | * @return int|null |
||
286 | */ |
||
287 | public function getChildrenCount() |
||
291 | |||
292 | /** |
||
293 | * Retrieve the popularity property |
||
294 | * |
||
295 | * @note Property available during /model/{model_id} request. |
||
296 | * for list of children models in parent model. |
||
297 | * |
||
298 | * @return float|null |
||
299 | */ |
||
300 | 1 | public function getPopularity() |
|
304 | } |
||
305 |
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.