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 ModelSingle 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 | /** |
||
| 46 | * Additional properties available during listed requests. |
||
| 47 | */ |
||
| 48 | protected $childrenCount = null; |
||
| 49 | |||
| 50 | protected $bigPhoto = null; |
||
| 51 | |||
| 52 | protected $previewPhoto = 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 | 'bigPhoto' => 'Yandex\Market\Content\Models\Base\Photo', |
||
| 60 | 'previewPhoto' => 'Yandex\Market\Content\Models\Base\Photo' |
||
| 61 | ]; |
||
| 62 | |||
| 63 | protected $propNameMap = [ |
||
| 64 | 'vendor' => 'vendorName', |
||
| 65 | 'modificationsCount' => 'childrenCount', |
||
| 66 | 'gradeCount' => 'gradesCount' |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor |
||
| 71 | * |
||
| 72 | * @param array $data |
||
| 73 | */ |
||
| 74 | 3 | public function __construct($data = array()) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Retrieve the offersCount property |
||
| 87 | * |
||
| 88 | * @return int|null |
||
| 89 | */ |
||
| 90 | 3 | public function getOffersCount() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Retrieve the rating property |
||
| 97 | * |
||
| 98 | * @return float|null |
||
| 99 | */ |
||
| 100 | 3 | public function getRating() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Retrieve the reviewsCount property |
||
| 107 | * |
||
| 108 | * @return int|null |
||
| 109 | */ |
||
| 110 | 3 | public function getReviewsCount() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Retrieve the articlesCount property |
||
| 117 | * |
||
| 118 | * @return int|null |
||
| 119 | */ |
||
| 120 | 2 | public function getArticlesCount() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Retrieve the isNew property |
||
| 127 | * |
||
| 128 | * @return bool|null |
||
| 129 | */ |
||
| 130 | 2 | public function getIsNew() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Retrieve the vendorId property |
||
| 145 | * |
||
| 146 | * @return int|null |
||
| 147 | */ |
||
| 148 | 3 | public function getVendorId() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Retrieve the gradesCount property |
||
| 155 | * |
||
| 156 | * @return int|null |
||
| 157 | */ |
||
| 158 | 2 | public function getGradesCount() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieve the categoryId property |
||
| 165 | * |
||
| 166 | * @return int|null |
||
| 167 | */ |
||
| 168 | 3 | public function getCategoryId() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Retrieve the id property |
||
| 175 | * |
||
| 176 | * @return int|null |
||
| 177 | */ |
||
| 178 | 3 | 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 | 3 | public function getLink() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Retrieve the isGroup property |
||
| 205 | * |
||
| 206 | * @return bool|null |
||
| 207 | */ |
||
| 208 | 2 | public function getIsGroup() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Retrieve the vendorName property |
||
| 215 | * @return string|null |
||
| 216 | */ |
||
| 217 | 2 | public function getVendorName() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Retrieve the name property |
||
| 224 | * @return string|null |
||
| 225 | */ |
||
| 226 | 3 | public function getName() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Retrieve the prices property |
||
| 233 | * |
||
| 234 | * @return Prices|null |
||
| 235 | */ |
||
| 236 | 3 | public function getPrices() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieve the description property |
||
| 243 | * @return string|null |
||
| 244 | */ |
||
| 245 | 3 | 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 | 3 | public function getMainPhoto() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Retrieve the childrenCount property |
||
| 272 | * |
||
| 273 | * @note Property available during /category/{category_id}/models request. |
||
| 274 | * |
||
| 275 | * @return int|null |
||
| 276 | */ |
||
| 277 | public function getChildrenCount() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Retrieve the bigPhoto property |
||
| 284 | * |
||
| 285 | * @note Property available during /search request. |
||
| 286 | * |
||
| 287 | * @return int|null |
||
| 288 | */ |
||
| 289 | public function getBigPhoto() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Retrieve the previewPhoto property |
||
| 296 | * |
||
| 297 | * @note Property available during /search request. |
||
| 298 | * |
||
| 299 | * @return int|null |
||
| 300 | */ |
||
| 301 | 1 | public function getPreviewPhoto() |
|
| 305 | } |
||
| 306 |
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.