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 DistanceMatrixRequest 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 DistanceMatrixRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class DistanceMatrixRequest |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Coordinate[]|string[] |
||
| 25 | */ |
||
| 26 | private $origins = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Coordinate[]|string[] |
||
| 30 | */ |
||
| 31 | private $destinations = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \DateTime|null |
||
| 35 | */ |
||
| 36 | private $departureTime; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \DateTime|null |
||
| 40 | */ |
||
| 41 | private $arrivalTime; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string|null |
||
| 45 | */ |
||
| 46 | private $travelMode; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool|null |
||
| 50 | */ |
||
| 51 | private $avoidHighways; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool|null |
||
| 55 | */ |
||
| 56 | private $avoidTolls; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | private $region; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string|null |
||
| 65 | */ |
||
| 66 | private $unitSystem; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string|null |
||
| 70 | */ |
||
| 71 | private $language; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param Coordinate[]|string[] $origins |
||
| 75 | * @param Coordinate[]|string[] $destinations |
||
| 76 | */ |
||
| 77 | public function __construct(array $origins, array $destinations) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasOrigins() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return Coordinate[]|string[] |
||
| 93 | */ |
||
| 94 | public function getOrigins() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Coordinate[]|string[] $origins |
||
| 101 | */ |
||
| 102 | public function setOrigins(array $origins) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param Coordinate[]|string[] $origins |
||
| 110 | */ |
||
| 111 | public function addOrigins(array $origins) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param Coordinate|string $origin |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function hasOrigin($origin) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Coordinate|string $origin |
||
| 130 | */ |
||
| 131 | public function addOrigin($origin) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param Coordinate|string $origin |
||
| 140 | */ |
||
| 141 | public function removeOrigin($origin) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function hasDestinations() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Coordinate[]|string[] |
||
| 157 | */ |
||
| 158 | public function getDestinations() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param Coordinate[]|string[] $destinations |
||
| 165 | */ |
||
| 166 | public function setDestinations(array $destinations) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Coordinate[]|string[] $destinations |
||
| 174 | */ |
||
| 175 | public function addDestinations(array $destinations) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Coordinate|string $destination |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function hasDestination($destination) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param Coordinate|string $destination |
||
| 194 | */ |
||
| 195 | public function addDestination($destination) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param Coordinate|string $destination |
||
| 204 | */ |
||
| 205 | public function removeDestination($destination) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | public function hasDepartureTime() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return \DateTime|null |
||
| 221 | */ |
||
| 222 | public function getDepartureTime() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \DateTime|null $departureTime |
||
| 229 | */ |
||
| 230 | public function setDepartureTime(\DateTime $departureTime = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function hasArrivalTime() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return \DateTime|null |
||
| 245 | */ |
||
| 246 | public function getArrivalTime() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param \DateTime|null $arrivalTime |
||
| 253 | */ |
||
| 254 | public function setArrivalTime(\DateTime $arrivalTime = null) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function hasTravelMode() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string|null |
||
| 269 | */ |
||
| 270 | public function getTravelMode() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string|null $travelMode |
||
| 277 | */ |
||
| 278 | public function setTravelMode($travelMode = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function hasAvoidHighways() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return bool|null |
||
| 293 | */ |
||
| 294 | public function getAvoidHighways() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param bool|null $avoidHighways |
||
| 301 | */ |
||
| 302 | public function setAvoidHighways($avoidHighways = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function hasAvoidTolls() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return bool|null |
||
| 317 | */ |
||
| 318 | public function getAvoidTolls() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param bool|null $avoidTolls |
||
| 325 | */ |
||
| 326 | public function setAvoidTolls($avoidTolls = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function hasRegion() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return string|null |
||
| 341 | */ |
||
| 342 | public function getRegion() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string|null $region |
||
| 349 | */ |
||
| 350 | public function setRegion($region = null) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function hasUnitSystem() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return string|null |
||
| 365 | */ |
||
| 366 | public function getUnitSystem() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string|null $unitSystem |
||
| 373 | */ |
||
| 374 | public function setUnitSystem($unitSystem = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function hasLanguage() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return string|null |
||
| 389 | */ |
||
| 390 | public function getLanguage() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string|null $language |
||
| 397 | */ |
||
| 398 | public function setLanguage($language = null) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return mixed[] |
||
| 405 | */ |
||
| 406 | public function buildQuery() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param Coordinate[]|string[] $places |
||
| 448 | * |
||
| 449 | * @return string[] |
||
| 450 | */ |
||
| 451 | private function buildPlaces(array $places) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param Coordinate|string $place |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | private function buildPlace($place) |
||
| 475 | } |
||
| 476 |
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.