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 | public function __construct(LocationInterface $origin, LocationInterface $destination) |
||
107 | |||
108 | /** |
||
109 | * @return LocationInterface |
||
110 | */ |
||
111 | public function getOrigin() |
||
115 | |||
116 | /** |
||
117 | * @param LocationInterface $origin |
||
118 | */ |
||
119 | public function setOrigin(LocationInterface $origin) |
||
123 | |||
124 | /** |
||
125 | * @return LocationInterface |
||
126 | */ |
||
127 | public function getDestination() |
||
131 | |||
132 | /** |
||
133 | * @param LocationInterface $destination |
||
134 | */ |
||
135 | public function setDestination(LocationInterface $destination) |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function hasDepartureTime() |
||
147 | |||
148 | /** |
||
149 | * @return \DateTime|null |
||
150 | */ |
||
151 | public function getDepartureTime() |
||
155 | |||
156 | /** |
||
157 | * @param \DateTime|null $departureTime |
||
158 | */ |
||
159 | public function setDepartureTime(\DateTime $departureTime = null) |
||
163 | |||
164 | /** |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasArrivalTime() |
||
171 | |||
172 | /** |
||
173 | * @return \DateTime|null |
||
174 | */ |
||
175 | public function getArrivalTime() |
||
179 | |||
180 | /** |
||
181 | * @param \DateTime|null $arrivalTime |
||
182 | */ |
||
183 | public function setArrivalTime(\DateTime $arrivalTime = null) |
||
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function hasWaypoints() |
||
195 | |||
196 | /** |
||
197 | * @return DirectionWaypoint[] |
||
198 | */ |
||
199 | public function getWaypoints() |
||
203 | |||
204 | /** |
||
205 | * @param DirectionWaypoint[] $waypoints |
||
206 | */ |
||
207 | public function setWaypoints(array $waypoints) |
||
212 | |||
213 | /** |
||
214 | * @param DirectionWaypoint[] $waypoints |
||
215 | */ |
||
216 | public function addWaypoints(array $waypoints) |
||
222 | |||
223 | /** |
||
224 | * @param DirectionWaypoint $waypoint |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function hasWaypoint(DirectionWaypoint $waypoint) |
||
232 | |||
233 | /** |
||
234 | * @param DirectionWaypoint $waypoint |
||
235 | */ |
||
236 | public function addWaypoint(DirectionWaypoint $waypoint) |
||
242 | |||
243 | /** |
||
244 | * @param DirectionWaypoint $waypoint |
||
245 | */ |
||
246 | public function removeWaypoint(DirectionWaypoint $waypoint) |
||
250 | |||
251 | /** |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function hasOptimizeWaypoints() |
||
258 | |||
259 | /** |
||
260 | * @return bool|null |
||
261 | */ |
||
262 | public function getOptimizeWaypoints() |
||
266 | |||
267 | /** |
||
268 | * @param bool|null $optimizeWaypoints |
||
269 | */ |
||
270 | public function setOptimizeWaypoints($optimizeWaypoints = null) |
||
274 | |||
275 | /** |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function hasTravelMode() |
||
282 | |||
283 | /** |
||
284 | * @return string|null |
||
285 | */ |
||
286 | public function getTravelMode() |
||
290 | |||
291 | /** |
||
292 | * @param string|null $travelMode |
||
293 | */ |
||
294 | public function setTravelMode($travelMode = null) |
||
298 | |||
299 | /** |
||
300 | * @return bool |
||
301 | */ |
||
302 | public function hasAvoid() |
||
306 | |||
307 | /** |
||
308 | * @return string|null |
||
309 | */ |
||
310 | public function getAvoid() |
||
314 | |||
315 | /** |
||
316 | * @param string|null $avoid |
||
317 | */ |
||
318 | public function setAvoid($avoid = null) |
||
322 | |||
323 | /** |
||
324 | * @return bool |
||
325 | */ |
||
326 | public function hasProvideRouteAlternatives() |
||
330 | |||
331 | /** |
||
332 | * @return bool|null |
||
333 | */ |
||
334 | public function getProvideRouteAlternatives() |
||
338 | |||
339 | /** |
||
340 | * @param bool|null $provideRouteAlternatives |
||
341 | */ |
||
342 | public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
||
346 | |||
347 | /** |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function hasTrafficModel() |
||
354 | |||
355 | /** |
||
356 | * @return string|null |
||
357 | */ |
||
358 | public function getTrafficModel() |
||
362 | |||
363 | /** |
||
364 | * @param string|null $trafficModel |
||
365 | */ |
||
366 | public function setTrafficModel($trafficModel) |
||
370 | |||
371 | /** |
||
372 | * @return bool |
||
373 | */ |
||
374 | public function hasTransitModes() |
||
378 | |||
379 | /** |
||
380 | * @return string[] |
||
381 | */ |
||
382 | public function getTransitModes() |
||
386 | |||
387 | /** |
||
388 | * @param string[] $transitModes |
||
389 | */ |
||
390 | public function setTransitModes(array $transitModes) |
||
395 | |||
396 | /** |
||
397 | * @param string[] $transitModes |
||
398 | */ |
||
399 | public function addTransitModes(array $transitModes) |
||
405 | |||
406 | /** |
||
407 | * @param string $transitMode |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function hasTransitMode($transitMode) |
||
415 | |||
416 | /** |
||
417 | * @param string $transitMode |
||
418 | */ |
||
419 | public function addTransitMode($transitMode) |
||
425 | |||
426 | /** |
||
427 | * @param string $transitMode |
||
428 | */ |
||
429 | public function removeTransitMode($transitMode) |
||
434 | |||
435 | /** |
||
436 | * @return bool |
||
437 | */ |
||
438 | public function hasTransitRoutingPreference() |
||
442 | |||
443 | /** |
||
444 | * @return string|null |
||
445 | */ |
||
446 | public function getTransitRoutingPreference() |
||
450 | |||
451 | /** |
||
452 | * @param string|null $transitRoutingPreference |
||
453 | */ |
||
454 | public function setTransitRoutingPreference($transitRoutingPreference) |
||
458 | |||
459 | /** |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function hasRegion() |
||
466 | |||
467 | /** |
||
468 | * @return string|null |
||
469 | */ |
||
470 | public function getRegion() |
||
474 | |||
475 | /** |
||
476 | * @param string|null $region |
||
477 | */ |
||
478 | public function setRegion($region = null) |
||
482 | |||
483 | /** |
||
484 | * @return bool |
||
485 | */ |
||
486 | public function hasUnitSystem() |
||
490 | |||
491 | /** |
||
492 | * @return string|null |
||
493 | */ |
||
494 | public function getUnitSystem() |
||
498 | |||
499 | /** |
||
500 | * @param string|null $unitSystem |
||
501 | */ |
||
502 | public function setUnitSystem($unitSystem = null) |
||
506 | |||
507 | /** |
||
508 | * @return bool |
||
509 | */ |
||
510 | public function hasLanguage() |
||
514 | |||
515 | /** |
||
516 | * @return string|null |
||
517 | */ |
||
518 | public function getLanguage() |
||
522 | |||
523 | /** |
||
524 | * @param string|null $language |
||
525 | */ |
||
526 | public function setLanguage($language = null) |
||
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | */ |
||
534 | public function build() |
||
601 | } |
||
602 |