Complex classes like DirectionRequest 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 DirectionRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class DirectionRequest implements DirectionRequestInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var LocationInterface |
||
| 25 | */ |
||
| 26 | private $origin; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var LocationInterface |
||
| 30 | */ |
||
| 31 | private $destination; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \DateTime|null |
||
| 35 | */ |
||
| 36 | private $departureTime; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \DateTime|null |
||
| 40 | */ |
||
| 41 | private $arrivalTime; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var DirectionWaypoint[] |
||
| 45 | */ |
||
| 46 | private $waypoints = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool|null |
||
| 50 | */ |
||
| 51 | private $optimizeWaypoints; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string|null |
||
| 55 | */ |
||
| 56 | private $travelMode; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | private $avoid; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool|null |
||
| 65 | */ |
||
| 66 | private $provideRouteAlternatives; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string|null |
||
| 70 | */ |
||
| 71 | private $trafficModel; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string[] |
||
| 75 | */ |
||
| 76 | private $transitModes = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string|null |
||
| 80 | */ |
||
| 81 | private $transitRoutingPreference; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string|null |
||
| 85 | */ |
||
| 86 | private $region; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string|null |
||
| 90 | */ |
||
| 91 | private $unitSystem; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string|null |
||
| 95 | */ |
||
| 96 | private $language; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param LocationInterface $origin |
||
| 100 | * @param LocationInterface $destination |
||
| 101 | */ |
||
| 102 | 472 | public function __construct(LocationInterface $origin, LocationInterface $destination) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return LocationInterface |
||
| 110 | */ |
||
| 111 | 8 | public function getOrigin() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param LocationInterface $origin |
||
| 118 | */ |
||
| 119 | 472 | public function setOrigin(LocationInterface $origin) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return LocationInterface |
||
| 126 | */ |
||
| 127 | 8 | public function getDestination() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param LocationInterface $destination |
||
| 134 | */ |
||
| 135 | 472 | public function setDestination(LocationInterface $destination) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 348 | public function hasDepartureTime() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return \DateTime|null |
||
| 150 | */ |
||
| 151 | 12 | public function getDepartureTime() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param \DateTime|null $departureTime |
||
| 158 | */ |
||
| 159 | 44 | public function setDepartureTime(\DateTime $departureTime = null) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 348 | public function hasArrivalTime() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return \DateTime|null |
||
| 174 | */ |
||
| 175 | 12 | public function getArrivalTime() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param \DateTime|null $arrivalTime |
||
| 182 | */ |
||
| 183 | 44 | public function setArrivalTime(\DateTime $arrivalTime = null) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | 356 | public function hasWaypoints() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return DirectionWaypoint[] |
||
| 198 | */ |
||
| 199 | 20 | public function getWaypoints() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param DirectionWaypoint[] $waypoints |
||
| 206 | */ |
||
| 207 | 8 | public function setWaypoints(array $waypoints) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param DirectionWaypoint[] $waypoints |
||
| 215 | */ |
||
| 216 | 28 | public function addWaypoints(array $waypoints) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param DirectionWaypoint $waypoint |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 104 | public function hasWaypoint(DirectionWaypoint $waypoint) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param DirectionWaypoint $waypoint |
||
| 235 | */ |
||
| 236 | 104 | public function addWaypoint(DirectionWaypoint $waypoint) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param DirectionWaypoint $waypoint |
||
| 245 | */ |
||
| 246 | 4 | public function removeWaypoint(DirectionWaypoint $waypoint) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 12 | public function hasOptimizeWaypoints() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return bool|null |
||
| 262 | */ |
||
| 263 | 12 | public function getOptimizeWaypoints() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param bool|null $optimizeWaypoints |
||
| 270 | */ |
||
| 271 | 68 | public function setOptimizeWaypoints($optimizeWaypoints = null) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 348 | public function hasTravelMode() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @return string|null |
||
| 286 | */ |
||
| 287 | 12 | public function getTravelMode() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param string|null $travelMode |
||
| 294 | */ |
||
| 295 | 44 | public function setTravelMode($travelMode = null) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | 348 | public function hasAvoid() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @return string|null |
||
| 310 | */ |
||
| 311 | 12 | public function getAvoid() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @param string|null $avoid |
||
| 318 | */ |
||
| 319 | 28 | public function setAvoid($avoid = null) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 348 | public function hasProvideRouteAlternatives() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool|null |
||
| 334 | */ |
||
| 335 | 12 | public function getProvideRouteAlternatives() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param bool|null $provideRouteAlternatives |
||
| 342 | */ |
||
| 343 | 28 | public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 348 | public function hasTrafficModel() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return string|null |
||
| 358 | */ |
||
| 359 | 12 | public function getTrafficModel() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param string|null $trafficModel |
||
| 366 | */ |
||
| 367 | 12 | public function setTrafficModel($trafficModel) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | 356 | public function hasTransitModes() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @return string[] |
||
| 382 | */ |
||
| 383 | 20 | public function getTransitModes() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @param string[] $transitModes |
||
| 390 | */ |
||
| 391 | 12 | public function setTransitModes(array $transitModes) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string[] $transitModes |
||
| 399 | */ |
||
| 400 | 12 | public function addTransitModes(array $transitModes) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $transitMode |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | 20 | public function hasTransitMode($transitMode) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $transitMode |
||
| 419 | */ |
||
| 420 | 20 | public function addTransitMode($transitMode) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $transitMode |
||
| 429 | */ |
||
| 430 | 4 | public function removeTransitMode($transitMode) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | 348 | public function hasTransitRoutingPreference() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @return string|null |
||
| 446 | */ |
||
| 447 | 12 | public function getTransitRoutingPreference() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param string|null $transitRoutingPreference |
||
| 454 | */ |
||
| 455 | 12 | public function setTransitRoutingPreference($transitRoutingPreference) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | 348 | public function hasRegion() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @return string|null |
||
| 470 | */ |
||
| 471 | 12 | public function getRegion() |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param string|null $region |
||
| 478 | */ |
||
| 479 | 28 | public function setRegion($region = null) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | 348 | public function hasUnitSystem() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @return string|null |
||
| 494 | */ |
||
| 495 | 12 | public function getUnitSystem() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string|null $unitSystem |
||
| 502 | */ |
||
| 503 | 28 | public function setUnitSystem($unitSystem = null) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | 348 | public function hasLanguage() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @return string|null |
||
| 518 | */ |
||
| 519 | 12 | public function getLanguage() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @param string|null $language |
||
| 526 | */ |
||
| 527 | 28 | public function setLanguage($language = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * {@inheritdoc} |
||
| 534 | */ |
||
| 535 | 336 | public function buildQuery() |
|
| 602 | } |
||
| 603 |