Complex classes like DirectionsRequest 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 DirectionsRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DirectionsRequest |
||
22 | { |
||
23 | /** |
||
24 | * @var Coordinate|string |
||
25 | */ |
||
26 | private $origin; |
||
27 | |||
28 | /** |
||
29 | * @var Coordinate|string |
||
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 DirectionsWaypoint[] |
||
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 $region; |
||
72 | |||
73 | /** |
||
74 | * @var string|null |
||
75 | */ |
||
76 | private $unitSystem; |
||
77 | |||
78 | /** |
||
79 | * @var string|null |
||
80 | */ |
||
81 | private $language; |
||
82 | |||
83 | /** |
||
84 | * @param Coordinate|string $origin |
||
85 | * @param Coordinate|string $destination |
||
86 | */ |
||
87 | public function __construct($origin, $destination) |
||
92 | |||
93 | /** |
||
94 | * @return Coordinate|string |
||
95 | */ |
||
96 | public function getOrigin() |
||
100 | |||
101 | /** |
||
102 | * @param Coordinate|string $origin |
||
103 | */ |
||
104 | public function setOrigin($origin) |
||
108 | |||
109 | /** |
||
110 | * @return Coordinate|string |
||
111 | */ |
||
112 | public function getDestination() |
||
116 | |||
117 | /** |
||
118 | * @param Coordinate|string $destination |
||
119 | */ |
||
120 | public function setDestination($destination) |
||
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function hasDepartureTime() |
||
132 | |||
133 | /** |
||
134 | * @return \DateTime|null |
||
135 | */ |
||
136 | public function getDepartureTime() |
||
140 | |||
141 | /** |
||
142 | * @param \DateTime|null $departureTime |
||
143 | */ |
||
144 | public function setDepartureTime(\DateTime $departureTime = null) |
||
148 | |||
149 | /** |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function hasArrivalTime() |
||
156 | |||
157 | /** |
||
158 | * @return \DateTime|null |
||
159 | */ |
||
160 | public function getArrivalTime() |
||
164 | |||
165 | /** |
||
166 | * @param \DateTime|null $arrivalTime |
||
167 | */ |
||
168 | public function setArrivalTime(\DateTime $arrivalTime = null) |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function hasWaypoints() |
||
180 | |||
181 | /** |
||
182 | * @return DirectionsWaypoint[] |
||
183 | */ |
||
184 | public function getWaypoints() |
||
188 | |||
189 | /** |
||
190 | * @param DirectionsWaypoint[] $waypoints |
||
191 | */ |
||
192 | public function setWaypoints(array $waypoints) |
||
197 | |||
198 | /** |
||
199 | * @param DirectionsWaypoint[] $waypoints |
||
200 | */ |
||
201 | public function addWaypoints(array $waypoints) |
||
207 | |||
208 | /** |
||
209 | * @param DirectionsWaypoint $waypoint |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function hasWaypoint(DirectionsWaypoint $waypoint) |
||
217 | |||
218 | /** |
||
219 | * @param DirectionsWaypoint $waypoint |
||
220 | */ |
||
221 | public function addWaypoint(DirectionsWaypoint $waypoint) |
||
227 | |||
228 | /** |
||
229 | * @param DirectionsWaypoint $waypoint |
||
230 | */ |
||
231 | public function removeWaypoint(DirectionsWaypoint $waypoint) |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function hasOptimizeWaypoints() |
||
243 | |||
244 | /** |
||
245 | * @return bool|null |
||
246 | */ |
||
247 | public function getOptimizeWaypoints() |
||
251 | |||
252 | /** |
||
253 | * @param bool|null $optimizeWaypoints |
||
254 | */ |
||
255 | public function setOptimizeWaypoints($optimizeWaypoints = null) |
||
259 | |||
260 | /** |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function hasTravelMode() |
||
267 | |||
268 | /** |
||
269 | * @return string|null |
||
270 | */ |
||
271 | public function getTravelMode() |
||
275 | |||
276 | /** |
||
277 | * @param string|null $travelMode |
||
278 | */ |
||
279 | public function setTravelMode($travelMode = null) |
||
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function hasAvoid() |
||
291 | |||
292 | /** |
||
293 | * @return string|null |
||
294 | */ |
||
295 | public function getAvoid() |
||
299 | |||
300 | /** |
||
301 | * @param string|null $avoid |
||
302 | */ |
||
303 | public function setAvoid($avoid = null) |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | public function hasProvideRouteAlternatives() |
||
315 | |||
316 | /** |
||
317 | * @return bool|null |
||
318 | */ |
||
319 | public function getProvideRouteAlternatives() |
||
323 | |||
324 | /** |
||
325 | * @param bool|null $provideRouteAlternatives |
||
326 | */ |
||
327 | public function setProvideRouteAlternatives($provideRouteAlternatives = null) |
||
331 | |||
332 | /** |
||
333 | * @return bool |
||
334 | */ |
||
335 | public function hasRegion() |
||
339 | |||
340 | /** |
||
341 | * @return string|null |
||
342 | */ |
||
343 | public function getRegion() |
||
347 | |||
348 | /** |
||
349 | * @param string|null $region |
||
350 | */ |
||
351 | public function setRegion($region = null) |
||
355 | |||
356 | /** |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function hasUnitSystem() |
||
363 | |||
364 | /** |
||
365 | * @return string|null |
||
366 | */ |
||
367 | public function getUnitSystem() |
||
371 | |||
372 | /** |
||
373 | * @param string|null $unitSystem |
||
374 | */ |
||
375 | public function setUnitSystem($unitSystem = null) |
||
379 | |||
380 | /** |
||
381 | * @return bool |
||
382 | */ |
||
383 | public function hasLanguage() |
||
387 | |||
388 | /** |
||
389 | * @return string|null |
||
390 | */ |
||
391 | public function getLanguage() |
||
395 | |||
396 | /** |
||
397 | * @param string|null $language |
||
398 | */ |
||
399 | public function setLanguage($language = null) |
||
403 | |||
404 | /** |
||
405 | * @return mixed[] |
||
406 | */ |
||
407 | public function buildQuery() |
||
463 | |||
464 | /** |
||
465 | * @param Coordinate|string $place |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | private function buildPlace($place) |
||
477 | } |
||
478 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.