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:
Complex classes like CountryEntity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CountryEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class CountryEntity extends CoordinateLogic implements JsonSerializable, CoordinateCollectionInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var Coordinate |
||
| 18 | */ |
||
| 19 | protected $coordinate; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var BoundingBox |
||
| 23 | */ |
||
| 24 | protected $boundingBox; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var RegionCollection |
||
| 28 | */ |
||
| 29 | protected $regions; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Polygon |
||
| 33 | */ |
||
| 34 | protected $polygon; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var CountryNameCollection |
||
| 38 | */ |
||
| 39 | protected $names; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $shortCode; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $code; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $latitude; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $longitude; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $currency; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $continent; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $population; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $area; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $capital; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $timezone; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $north; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $east; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $south; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $west; |
||
| 110 | |||
| 111 | 4 | View Code Duplication | public function __construct() |
| 122 | |||
| 123 | /** |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function toArray() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function jsonSerialize() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Coordinate |
||
| 157 | */ |
||
| 158 | 4 | public function getCoordinate() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param Coordinate $coordinate |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function setCoordinate(Coordinate $coordinate) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return BoundingBox |
||
| 177 | */ |
||
| 178 | 4 | public function getBoundingBox() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param BoundingBox $boundingBox |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function setBoundingBox(BoundingBox $boundingBox) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return RegionCollection |
||
| 197 | */ |
||
| 198 | public function getRegions() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param RegionCollection $regions |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setRegions(RegionCollection $regions) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return Polygon |
||
| 217 | */ |
||
| 218 | public function getPolygon() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Polygon $polygon |
||
| 225 | * |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function setPolygon(Polygon $polygon) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return CountryNameCollection |
||
| 237 | */ |
||
| 238 | 4 | public function getNames() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param CountryNameCollection $names |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | 4 | public function setNames(CountryNameCollection $names) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getShortCode() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $shortCode |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | 4 | public function setShortCode($shortCode) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 3 | public function getCode() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $code |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | 4 | public function setCode($code) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getLatitude() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $latitude |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | 4 | public function setLatitude($latitude) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function getLongitude() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $longitude |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | 4 | public function setLongitude($longitude) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getCurrency() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $currency |
||
| 347 | * |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | 4 | public function setCurrency($currency) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getContinent() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $continent |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | 4 | public function setContinent($continent) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public function getPopulation() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $population |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | 4 | public function setPopulation($population) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getArea() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $area |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | 4 | public function setArea($area) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getCapital() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $capital |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | 4 | public function setCapital($capital) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getTimezone() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $timezone |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | 4 | public function setTimezone($timezone) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getNorth() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param string $north |
||
| 467 | * |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | 4 | public function setNorth($north) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getEast() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $east |
||
| 488 | * |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | 4 | public function setEast($east) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function getSouth() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $south |
||
| 509 | * |
||
| 510 | * @return $this |
||
| 511 | */ |
||
| 512 | 4 | public function setSouth($south) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function getWest() |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string $west |
||
| 530 | * |
||
| 531 | * @return $this |
||
| 532 | */ |
||
| 533 | 4 | public function setWest($west) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * {@inheritdoc} |
||
| 543 | */ |
||
| 544 | public function normalizeLatitude($latitude) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * {@inheritdoc} |
||
| 551 | */ |
||
| 552 | public function normalizeLongitude($longitude) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * {@inheritdoc} |
||
| 559 | */ |
||
| 560 | public function getEllipsoid() |
||
| 564 | } |
||
| 565 |
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.