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 DirectionService 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 DirectionService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DirectionService extends AbstractService |
||
40 | { |
||
41 | /** |
||
42 | * @param HttpClient $client |
||
43 | * @param MessageFactory $messageFactory |
||
44 | */ |
||
45 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
49 | |||
50 | /** |
||
51 | * @param DirectionRequestInterface $request |
||
52 | * |
||
53 | * @return DirectionResponse |
||
54 | */ |
||
55 | View Code Duplication | public function route(DirectionRequestInterface $request) |
|
62 | |||
63 | /** |
||
64 | * @param string $data |
||
65 | * |
||
66 | * @return mixed[] |
||
67 | */ |
||
68 | private function parse($data) |
||
81 | |||
82 | /** |
||
83 | * @param mixed[] $data |
||
84 | * |
||
85 | * @return DirectionResponse |
||
86 | */ |
||
87 | private function buildResponse(array $data) |
||
106 | |||
107 | /** |
||
108 | * @param mixed[] $data |
||
109 | * |
||
110 | * @return DirectionRoute[] |
||
111 | */ |
||
112 | private function buildRoutes(array $data) |
||
122 | |||
123 | /** |
||
124 | * @param mixed[] $data |
||
125 | * |
||
126 | * @return DirectionRoute |
||
127 | */ |
||
128 | private function buildRoute(array $data) |
||
157 | |||
158 | /** |
||
159 | * @param mixed[] $data |
||
160 | * |
||
161 | * @return DirectionGeocoded[] |
||
162 | */ |
||
163 | private function buildGeocodedWaypoints(array $data) |
||
173 | |||
174 | /** |
||
175 | * @param mixed[] $data |
||
176 | * |
||
177 | * @return DirectionGeocoded |
||
178 | */ |
||
179 | private function buildGeocodedWaypoint(array $data) |
||
195 | |||
196 | /** |
||
197 | * @param mixed[] $data |
||
198 | * |
||
199 | * @return DirectionLeg[] |
||
200 | */ |
||
201 | private function buildLegs(array $data) |
||
211 | |||
212 | /** |
||
213 | * @param mixed[] $data |
||
214 | * |
||
215 | * @return DirectionLeg |
||
216 | */ |
||
217 | private function buildLeg(array $data) |
||
246 | |||
247 | /** |
||
248 | * @param mixed[] $data |
||
249 | * |
||
250 | * @return DirectionStep[] |
||
251 | */ |
||
252 | private function buildSteps(array $data) |
||
262 | |||
263 | /** |
||
264 | * @param mixed[] $data |
||
265 | * |
||
266 | * @return DirectionStep |
||
267 | */ |
||
268 | private function buildStep(array $data) |
||
285 | |||
286 | /** |
||
287 | * @param mixed[] $data |
||
288 | * |
||
289 | * @return DirectionTransitDetails |
||
290 | */ |
||
291 | private function buildTransitDetails(array $data) |
||
308 | |||
309 | /** |
||
310 | * @param mixed[] $data |
||
311 | * |
||
312 | * @return DirectionTransitLine |
||
313 | */ |
||
314 | private function buildTransitLine(array $data) |
||
340 | |||
341 | /** |
||
342 | * @param mixed[] $data |
||
343 | * |
||
344 | * @return DirectionTransitAgency[] |
||
345 | */ |
||
346 | private function buildTransitAgencies(array $data) |
||
356 | |||
357 | /** |
||
358 | * @param mixed[] $data |
||
359 | * |
||
360 | * @return DirectionTransitAgency |
||
361 | */ |
||
362 | private function buildTransitAgency(array $data) |
||
371 | |||
372 | /** |
||
373 | * @param mixed[] $data |
||
374 | * |
||
375 | * @return DirectionTransitStop |
||
376 | */ |
||
377 | private function buildTransitStop(array $data) |
||
385 | |||
386 | /** |
||
387 | * @param mixed[] $data |
||
388 | * |
||
389 | * @return DirectionTransitVehicle |
||
390 | */ |
||
391 | private function buildTransitVehicle(array $data) |
||
400 | |||
401 | /** |
||
402 | * @param mixed[] $data |
||
403 | * |
||
404 | * @return Bound |
||
405 | */ |
||
406 | private function buildBound(array $data) |
||
413 | |||
414 | /** |
||
415 | * @param mixed[] $data |
||
416 | * |
||
417 | * @return Coordinate |
||
418 | */ |
||
419 | private function buildCoordinate(array $data) |
||
423 | |||
424 | /** |
||
425 | * @param mixed[] $data |
||
426 | * |
||
427 | * @return Distance |
||
428 | */ |
||
429 | private function buildDistance(array $data) |
||
433 | |||
434 | /** |
||
435 | * @param mixed[] $data |
||
436 | * |
||
437 | * @return Duration |
||
438 | */ |
||
439 | private function buildDuration(array $data) |
||
443 | |||
444 | /** |
||
445 | * @param string[] $data |
||
446 | * |
||
447 | * @return EncodedPolyline |
||
448 | */ |
||
449 | private function buildEncodedPolyline(array $data) |
||
453 | |||
454 | /** |
||
455 | * @param mixed[] $data |
||
456 | * |
||
457 | * @return Fare |
||
458 | */ |
||
459 | private function buildFare(array $data) |
||
463 | |||
464 | /** |
||
465 | * @param mixed[] $data |
||
466 | * |
||
467 | * @return Time |
||
468 | */ |
||
469 | private function buildTime(array $data) |
||
477 | } |
||
478 |
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.