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 Directions 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 Directions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Directions extends AbstractService |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @param HttpClient $client |
||
| 30 | * @param MessageFactory $messageFactory |
||
| 31 | */ |
||
| 32 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param DirectionsRequest $request |
||
| 39 | * |
||
| 40 | * @return DirectionsResponse |
||
| 41 | */ |
||
| 42 | View Code Duplication | public function route(DirectionsRequest $request) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $data |
||
| 52 | * |
||
| 53 | * @return mixed[] |
||
| 54 | */ |
||
| 55 | private function parse($data) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param mixed[] $data |
||
| 71 | * |
||
| 72 | * @return DirectionsResponse |
||
| 73 | */ |
||
| 74 | private function buildResponse(array $data) |
||
| 75 | { |
||
| 76 | $response = new DirectionsResponse(); |
||
| 77 | $response->setStatus($data['status']); |
||
| 78 | $response->setRoutes(isset($data['routes']) ? $this->buildRoutes($data['routes']) : []); |
||
| 79 | |||
| 80 | $response->setGeocodedWaypoints( |
||
| 81 | isset($data['geocoded_waypoints']) ? $this->buildGeocodedWaypoints($data['geocoded_waypoints']) : [] |
||
| 82 | ); |
||
| 83 | |||
| 84 | $response->setAvailableTravelModes( |
||
| 85 | isset($data['available_travel_modes']) ? $data['available_travel_modes'] : [] |
||
| 86 | ); |
||
| 87 | |||
| 88 | return $response; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param mixed[] $data |
||
| 93 | * |
||
| 94 | * @return DirectionsRoute[] |
||
| 95 | */ |
||
| 96 | private function buildRoutes(array $data) |
||
| 97 | { |
||
| 98 | $routes = []; |
||
| 99 | foreach ($data as $item) { |
||
| 100 | $routes[] = $this->buildRoute($item); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $routes; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param mixed[] $data |
||
| 108 | * |
||
| 109 | * @return DirectionsRoute |
||
| 110 | */ |
||
| 111 | private function buildRoute(array $data) |
||
| 112 | { |
||
| 113 | $route = new DirectionsRoute(); |
||
| 114 | $route->setBound($this->buildBound($data['bounds'])); |
||
| 115 | $route->setCopyrights(isset($data['copyrights']) ? $data['copyrights'] : null); |
||
| 116 | $route->setLegs($this->buildLegs($data['legs'])); |
||
| 117 | $route->setOverviewPolyline($this->buildEncodedPolyline($data['overview_polyline'])); |
||
| 118 | $route->setSummary(isset($data['summary']) ? $data['summary'] : null); |
||
| 119 | $route->setFare(isset($data['fare']) ? $this->buildFare($data['fare']) : null); |
||
| 120 | $route->setWarnings(isset($data['warnings']) ? $data['warnings'] : []); |
||
| 121 | $route->setWaypointOrders(isset($data['waypoint_order']) ? $data['waypoint_order'] : []); |
||
| 122 | |||
| 123 | return $route; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param mixed[] $data |
||
| 128 | * |
||
| 129 | * @return DirectionsGeocoded[] |
||
| 130 | */ |
||
| 131 | private function buildGeocodedWaypoints(array $data) |
||
| 132 | { |
||
| 133 | $geocodedWaypoints = []; |
||
| 134 | foreach ($data as $item) { |
||
| 135 | $geocodedWaypoints[] = $this->buildGeocodedWaypoint($item); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $geocodedWaypoints; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param mixed[] $data |
||
| 143 | * |
||
| 144 | * @return DirectionsGeocoded |
||
| 145 | */ |
||
| 146 | private function buildGeocodedWaypoint(array $data) |
||
| 147 | { |
||
| 148 | $geocodedWaypoint = new DirectionsGeocoded(); |
||
| 149 | $geocodedWaypoint->setStatus($data['geocoder_status']); |
||
| 150 | $geocodedWaypoint->setPartialMatch(isset($data['partial_match']) ? $data['partial_match'] : null); |
||
| 151 | $geocodedWaypoint->setPlaceId(isset($data['place_id']) ? $data['place_id'] : null); |
||
| 152 | $geocodedWaypoint->setTypes($data['types']); |
||
| 153 | |||
| 154 | return $geocodedWaypoint; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param mixed[] $data |
||
| 159 | * |
||
| 160 | * @return DirectionsLeg[] |
||
| 161 | */ |
||
| 162 | private function buildLegs(array $data) |
||
| 163 | { |
||
| 164 | $legs = []; |
||
| 165 | foreach ($data as $item) { |
||
| 166 | $legs[] = $this->buildLeg($item); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $legs; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param mixed[] $data |
||
| 174 | * |
||
| 175 | * @return DirectionsLeg |
||
| 176 | */ |
||
| 177 | private function buildLeg(array $data) |
||
| 178 | { |
||
| 179 | $leg = new DirectionsLeg(); |
||
| 180 | $leg->setDistance($this->buildDistance($data['distance'])); |
||
| 181 | $leg->setDuration($this->buildDuration($data['duration'])); |
||
| 182 | $leg->setDepartureTime(isset($data['departure_time']) ? $this->buildDateTime($data['departure_time']) : null); |
||
| 183 | $leg->setArrivalTime(isset($data['arrival_time']) ? $this->buildDateTime($data['arrival_time']) : null); |
||
| 184 | $leg->setEndAddress($data['end_address']); |
||
| 185 | $leg->setEndLocation($this->buildCoordinate($data['end_location'])); |
||
| 186 | $leg->setStartAddress($data['start_address']); |
||
| 187 | $leg->setStartLocation($this->buildCoordinate($data['start_location'])); |
||
| 188 | $leg->setSteps($this->buildSteps($data['steps'])); |
||
| 189 | $leg->setViaWaypoints(isset($data['via_waypoint']) ? $data['via_waypoint'] : []); |
||
| 190 | $leg->setDurationInTraffic( |
||
| 191 | isset($data['duration_in_traffic']) ? $this->buildDuration($data['duration_in_traffic']) : null |
||
| 192 | ); |
||
| 193 | |||
| 194 | return $leg; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @codeCoverageIgnore |
||
| 199 | * |
||
| 200 | * @param mixed[] $data |
||
| 201 | * |
||
| 202 | * @return DirectionsFare |
||
| 203 | */ |
||
| 204 | private function buildFare(array $data) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param mixed[] $data |
||
| 216 | * |
||
| 217 | * @return DirectionsStep[] |
||
| 218 | */ |
||
| 219 | private function buildSteps(array $data) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param mixed[] $data |
||
| 231 | * |
||
| 232 | * @return DirectionsStep |
||
| 233 | */ |
||
| 234 | private function buildStep(array $data) |
||
| 235 | { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param mixed[] $data |
||
| 253 | * |
||
| 254 | * @return DirectionsTransitDetails |
||
| 255 | */ |
||
| 256 | private function buildTransitDetails(array $data) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param mixed[] $data |
||
| 273 | * |
||
| 274 | * @return DirectionsTransitLine |
||
| 275 | */ |
||
| 276 | private function buildTransitLine(array $data) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param mixed[] $data |
||
| 293 | * |
||
| 294 | * @return DirectionsTransitAgency[] |
||
| 295 | */ |
||
| 296 | private function buildTransitAgencies(array $data) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param mixed[] $data |
||
| 309 | * |
||
| 310 | * @return DirectionsTransitAgency |
||
| 311 | */ |
||
| 312 | private function buildTransitAgency(array $data) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param mixed[] $data |
||
| 324 | * |
||
| 325 | * @return DirectionsTransitStop |
||
| 326 | */ |
||
| 327 | private function buildTransitStop(array $data) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param mixed[] $data |
||
| 338 | * |
||
| 339 | * @return DirectionsTransitVehicle |
||
| 340 | */ |
||
| 341 | private function buildTransitVehicle(array $data) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param mixed[] $data |
||
| 353 | * |
||
| 354 | * @return \DateTime |
||
| 355 | */ |
||
| 356 | private function buildDateTime(array $data) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param mixed[] $data |
||
| 363 | * |
||
| 364 | * @return Bound |
||
| 365 | */ |
||
| 366 | private function buildBound(array $data) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param mixed[] $data |
||
| 376 | * |
||
| 377 | * @return Coordinate |
||
| 378 | */ |
||
| 379 | private function buildCoordinate(array $data) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param mixed[] $data |
||
| 386 | * |
||
| 387 | * @return Distance |
||
| 388 | */ |
||
| 389 | private function buildDistance(array $data) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param mixed[] $data |
||
| 396 | * |
||
| 397 | * @return Duration |
||
| 398 | */ |
||
| 399 | private function buildDuration(array $data) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string[] $data |
||
| 406 | * |
||
| 407 | * @return EncodedPolyline |
||
| 408 | */ |
||
| 409 | private function buildEncodedPolyline(array $data) |
||
| 413 | } |
||
| 414 |
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.