Complex classes like Direction 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 Direction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Direction extends AbstractService |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @param HttpClient $client |
||
| 42 | * @param MessageFactory $messageFactory |
||
| 43 | */ |
||
| 44 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param DirectionRequestInterface $request |
||
| 51 | * |
||
| 52 | * @return DirectionResponse |
||
| 53 | */ |
||
| 54 | public function route(DirectionRequestInterface $request) |
||
| 55 | { |
||
| 56 | $response = $this->getClient()->sendRequest($this->createRequest($request->build())); |
||
| 57 | $data = $this->parse((string) $response->getBody()); |
||
| 58 | |||
| 59 | return $this->buildResponse($data); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $data |
||
| 64 | * |
||
| 65 | * @return mixed[] |
||
| 66 | */ |
||
| 67 | private function parse($data) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param mixed[] $data |
||
| 83 | * |
||
| 84 | * @return DirectionResponse |
||
| 85 | */ |
||
| 86 | private function buildResponse(array $data) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param mixed[] $data |
||
| 108 | * |
||
| 109 | * @return DirectionRoute[] |
||
| 110 | */ |
||
| 111 | private function buildRoutes(array $data) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param mixed[] $data |
||
| 124 | * |
||
| 125 | * @return DirectionRoute |
||
| 126 | */ |
||
| 127 | private function buildRoute(array $data) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param mixed[] $data |
||
| 159 | * |
||
| 160 | * @return DirectionGeocoded[] |
||
| 161 | */ |
||
| 162 | private function buildGeocodedWaypoints(array $data) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param mixed[] $data |
||
| 175 | * |
||
| 176 | * @return DirectionGeocoded |
||
| 177 | */ |
||
| 178 | private function buildGeocodedWaypoint(array $data) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param mixed[] $data |
||
| 197 | * |
||
| 198 | * @return DirectionLeg[] |
||
| 199 | */ |
||
| 200 | private function buildLegs(array $data) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param mixed[] $data |
||
| 213 | * |
||
| 214 | * @return DirectionLeg |
||
| 215 | */ |
||
| 216 | private function buildLeg(array $data) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param mixed[] $data |
||
| 248 | * |
||
| 249 | * @return DirectionStep[] |
||
| 250 | */ |
||
| 251 | private function buildSteps(array $data) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param mixed[] $data |
||
| 264 | * |
||
| 265 | * @return DirectionStep |
||
| 266 | */ |
||
| 267 | private function buildStep(array $data) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param mixed[] $data |
||
| 287 | * |
||
| 288 | * @return DirectionTransitDetails |
||
| 289 | */ |
||
| 290 | private function buildTransitDetails(array $data) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param mixed[] $data |
||
| 310 | * |
||
| 311 | * @return DirectionTransitLine |
||
| 312 | */ |
||
| 313 | private function buildTransitLine(array $data) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param mixed[] $data |
||
| 342 | * |
||
| 343 | * @return DirectionTransitAgency[] |
||
| 344 | */ |
||
| 345 | private function buildTransitAgencies(array $data) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param mixed[] $data |
||
| 358 | * |
||
| 359 | * @return DirectionTransitAgency |
||
| 360 | */ |
||
| 361 | private function buildTransitAgency(array $data) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param mixed[] $data |
||
| 373 | * |
||
| 374 | * @return DirectionTransitStop |
||
| 375 | */ |
||
| 376 | private function buildTransitStop(array $data) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param mixed[] $data |
||
| 387 | * |
||
| 388 | * @return DirectionTransitVehicle |
||
| 389 | */ |
||
| 390 | private function buildTransitVehicle(array $data) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param mixed[] $data |
||
| 402 | * |
||
| 403 | * @return \DateTime |
||
| 404 | */ |
||
| 405 | private function buildDateTime(array $data) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param mixed[] $data |
||
| 412 | * |
||
| 413 | * @return Bound |
||
| 414 | */ |
||
| 415 | private function buildBound(array $data) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param mixed[] $data |
||
| 425 | * |
||
| 426 | * @return Coordinate |
||
| 427 | */ |
||
| 428 | private function buildCoordinate(array $data) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param mixed[] $data |
||
| 435 | * |
||
| 436 | * @return Distance |
||
| 437 | */ |
||
| 438 | private function buildDistance(array $data) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param mixed[] $data |
||
| 445 | * |
||
| 446 | * @return Duration |
||
| 447 | */ |
||
| 448 | private function buildDuration(array $data) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string[] $data |
||
| 455 | * |
||
| 456 | * @return EncodedPolyline |
||
| 457 | */ |
||
| 458 | private function buildEncodedPolyline(array $data) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param mixed[] $data |
||
| 465 | * |
||
| 466 | * @return Fare |
||
| 467 | */ |
||
| 468 | private function buildFare(array $data) |
||
| 472 | } |
||
| 473 |