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 | 192 | public function __construct(LocationInterface $origin, LocationInterface $destination) |
|
103 | |||
104 | /** |
||
105 | * @return LocationInterface |
||
106 | */ |
||
107 | 8 | public function getOrigin() |
|
111 | |||
112 | 192 | public function setOrigin(LocationInterface $origin) |
|
116 | |||
117 | /** |
||
118 | * @return LocationInterface |
||
119 | */ |
||
120 | 8 | public function getDestination() |
|
124 | |||
125 | 192 | public function setDestination(LocationInterface $destination) |
|
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | 68 | public function hasDepartureTime() |
|
137 | |||
138 | /** |
||
139 | * @return \DateTime|null |
||
140 | */ |
||
141 | 12 | public function getDepartureTime() |
|
145 | |||
146 | 12 | public function setDepartureTime(\DateTime $departureTime = null) |
|
150 | |||
151 | /** |
||
152 | * @return bool |
||
153 | */ |
||
154 | 68 | public function hasArrivalTime() |
|
158 | |||
159 | /** |
||
160 | * @return \DateTime|null |
||
161 | */ |
||
162 | 12 | public function getArrivalTime() |
|
166 | |||
167 | 12 | public function setArrivalTime(\DateTime $arrivalTime = null) |
|
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | */ |
||
175 | 76 | public function hasWaypoints() |
|
179 | |||
180 | /** |
||
181 | * @return DirectionWaypoint[] |
||
182 | */ |
||
183 | 20 | public function getWaypoints() |
|
187 | |||
188 | /** |
||
189 | * @param DirectionWaypoint[] $waypoints |
||
190 | */ |
||
191 | 8 | public function setWaypoints(array $waypoints) |
|
196 | |||
197 | /** |
||
198 | * @param DirectionWaypoint[] $waypoints |
||
199 | */ |
||
200 | 12 | public function addWaypoints(array $waypoints) |
|
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | 24 | public function hasWaypoint(DirectionWaypoint $waypoint) |
|
214 | |||
215 | 24 | public function addWaypoint(DirectionWaypoint $waypoint) |
|
221 | |||
222 | 4 | public function removeWaypoint(DirectionWaypoint $waypoint) |
|
227 | |||
228 | /** |
||
229 | * @return bool |
||
230 | */ |
||
231 | 12 | public function hasOptimizeWaypoints() |
|
235 | |||
236 | /** |
||
237 | * @return bool|null |
||
238 | */ |
||
239 | 12 | public function getOptimizeWaypoints() |
|
243 | |||
244 | /** |
||
245 | * @param bool|null $optimizeWaypoints |
||
246 | */ |
||
247 | 12 | public function setOptimizeWaypoints($optimizeWaypoints = null) |
|
251 | |||
252 | /** |
||
253 | * @return bool |
||
254 | */ |
||
255 | 68 | public function hasTravelMode() |
|
259 | |||
260 | /** |
||
261 | * @return string|null |
||
262 | */ |
||
263 | 12 | public function getTravelMode() |
|
267 | |||
268 | /** |
||
269 | * @param string|null $travelMode |
||
270 | */ |
||
271 | 12 | public function setTravelMode($travelMode = null) |
|
275 | |||
276 | /** |
||
277 | * @return bool |
||
278 | */ |
||
279 | 68 | public function hasAvoid() |
|
283 | |||
284 | /** |
||
285 | * @return string|null |
||
286 | */ |
||
287 | 12 | public function getAvoid() |
|
291 | |||
292 | /** |
||
293 | * @param string|null $avoid |
||
294 | */ |
||
295 | 12 | public function setAvoid($avoid = null) |
|
299 | |||
300 | /** |
||
301 | * @return bool |
||
302 | */ |
||
303 | 68 | public function hasProvideRouteAlternatives() |
|
307 | |||
308 | /** |
||
309 | * @return bool|null |
||
310 | */ |
||
311 | 12 | public function getProvideRouteAlternatives() |
|
315 | |||
316 | /** |
||
317 | * @param bool|null $provideRouteAlternatives |
||
318 | */ |
||
319 | 12 | public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
|
323 | |||
324 | /** |
||
325 | * @return bool |
||
326 | */ |
||
327 | 68 | public function hasTrafficModel() |
|
331 | |||
332 | /** |
||
333 | * @return string|null |
||
334 | */ |
||
335 | 12 | public function getTrafficModel() |
|
339 | |||
340 | /** |
||
341 | * @param string|null $trafficModel |
||
342 | */ |
||
343 | 12 | public function setTrafficModel($trafficModel) |
|
347 | |||
348 | /** |
||
349 | * @return bool |
||
350 | */ |
||
351 | 76 | public function hasTransitModes() |
|
355 | |||
356 | /** |
||
357 | * @return string[] |
||
358 | */ |
||
359 | 20 | public function getTransitModes() |
|
363 | |||
364 | /** |
||
365 | * @param string[] $transitModes |
||
366 | */ |
||
367 | 12 | public function setTransitModes(array $transitModes) |
|
372 | |||
373 | /** |
||
374 | * @param string[] $transitModes |
||
375 | */ |
||
376 | 12 | public function addTransitModes(array $transitModes) |
|
382 | |||
383 | /** |
||
384 | * @param string $transitMode |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | 20 | public function hasTransitMode($transitMode) |
|
392 | |||
393 | /** |
||
394 | * @param string $transitMode |
||
395 | */ |
||
396 | 20 | public function addTransitMode($transitMode) |
|
402 | |||
403 | /** |
||
404 | * @param string $transitMode |
||
405 | */ |
||
406 | 4 | public function removeTransitMode($transitMode) |
|
411 | |||
412 | /** |
||
413 | * @return bool |
||
414 | */ |
||
415 | 68 | public function hasTransitRoutingPreference() |
|
419 | |||
420 | /** |
||
421 | * @return string|null |
||
422 | */ |
||
423 | 12 | public function getTransitRoutingPreference() |
|
427 | |||
428 | /** |
||
429 | * @param string|null $transitRoutingPreference |
||
430 | */ |
||
431 | 12 | public function setTransitRoutingPreference($transitRoutingPreference) |
|
435 | |||
436 | /** |
||
437 | * @return bool |
||
438 | */ |
||
439 | 68 | public function hasRegion() |
|
443 | |||
444 | /** |
||
445 | * @return string|null |
||
446 | */ |
||
447 | 12 | public function getRegion() |
|
451 | |||
452 | /** |
||
453 | * @param string|null $region |
||
454 | */ |
||
455 | 12 | public function setRegion($region = null) |
|
459 | |||
460 | /** |
||
461 | * @return bool |
||
462 | */ |
||
463 | 68 | public function hasUnitSystem() |
|
467 | |||
468 | /** |
||
469 | * @return string|null |
||
470 | */ |
||
471 | 12 | public function getUnitSystem() |
|
475 | |||
476 | /** |
||
477 | * @param string|null $unitSystem |
||
478 | */ |
||
479 | 12 | public function setUnitSystem($unitSystem = null) |
|
483 | |||
484 | /** |
||
485 | * @return bool |
||
486 | */ |
||
487 | 68 | public function hasLanguage() |
|
491 | |||
492 | /** |
||
493 | * @return string|null |
||
494 | */ |
||
495 | 12 | public function getLanguage() |
|
499 | |||
500 | /** |
||
501 | * @param string|null $language |
||
502 | */ |
||
503 | 12 | public function setLanguage($language = null) |
|
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | 56 | public function buildQuery() |
|
578 | } |
||
579 |