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 RegionEntity 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 RegionEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class RegionEntity extends CoordinateLogic implements JsonSerializable, CoordinateCollectionInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Coordinate |
||
| 20 | */ |
||
| 21 | protected $coordinate; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var BoundingBox |
||
| 25 | */ |
||
| 26 | protected $boundingBox; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var RegionNameCollection |
||
| 30 | */ |
||
| 31 | protected $names; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var CountryEntity |
||
| 35 | */ |
||
| 36 | protected $country; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Polygon |
||
| 40 | */ |
||
| 41 | protected $polygon; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $unmappedCountry; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $code; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $longCode; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var TypeInterface|string |
||
| 60 | */ |
||
| 61 | protected $type; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $timezone; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $latitude; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $longitude; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $north; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $east; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $south; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $west; |
||
| 97 | |||
| 98 | 4 | View Code Duplication | public function __construct() |
| 108 | |||
| 109 | /** |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function jsonSerialize() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return Polygon |
||
| 132 | */ |
||
| 133 | public function getPolygon() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param Polygon $polygon |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function setPolygon(Polygon $polygon) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return BoundingBox |
||
| 152 | */ |
||
| 153 | 4 | public function getBoundingBox() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param BoundingBox $boundingBox |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function setBoundingBox(BoundingBox $boundingBox) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 3 | public function getCode() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $code |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 4 | public function setCode($code) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getLongCode() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $longCode |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | 4 | public function setLongCode($longCode) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return Coordinate |
||
| 212 | */ |
||
| 213 | public function getCoordinate() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param Coordinate $coordinate |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function setCoordinate(Coordinate $coordinate) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getLatitude() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $latitude |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | 4 | public function setLatitude($latitude) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getLongitude() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $longitude |
||
| 261 | * |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | 4 | public function setLongitude($longitude) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return RegionNameCollection |
||
| 274 | */ |
||
| 275 | 4 | public function getNames() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param RegionNameCollection $names |
||
| 282 | * |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 4 | public function setNames(RegionNameCollection $names) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getTimezone() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $timezone |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | 4 | public function setTimezone($timezone) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @return TypeInterface|string |
||
| 314 | */ |
||
| 315 | public function getType() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param TypeInterface|string $type |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | 4 | public function setType($type) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getNorth() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $north |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | 4 | public function setNorth($north) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function getEast() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $east |
||
| 363 | * |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | 4 | public function setEast($east) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getSouth() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $south |
||
| 384 | * |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | 4 | public function setSouth($south) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getWest() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $west |
||
| 405 | * |
||
| 406 | * @return $this |
||
| 407 | */ |
||
| 408 | 4 | public function setWest($west) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @return CountryEntity |
||
| 418 | */ |
||
| 419 | public function getCountry() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param CountryEntity|string $country |
||
| 430 | * |
||
| 431 | * @return $this |
||
| 432 | */ |
||
| 433 | public function setCountry($country) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getUnmappedCountry() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $unmappedCountry |
||
| 454 | * |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | 4 | public function setUnmappedCountry($unmappedCountry) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * {@inheritdoc} |
||
| 466 | */ |
||
| 467 | public function normalizeLatitude($latitude) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | */ |
||
| 475 | public function normalizeLongitude($longitude) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * {@inheritdoc} |
||
| 482 | */ |
||
| 483 | public function getEllipsoid() |
||
| 487 | } |
||
| 488 |
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.