Complex classes like DirectionLeg 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 DirectionLeg, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DirectionLeg |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Distance|null |
||
| 28 | */ |
||
| 29 | private $distance; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Duration|null |
||
| 33 | */ |
||
| 34 | private $duration; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Duration|null |
||
| 38 | */ |
||
| 39 | private $durationInTraffic; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Time|null |
||
| 43 | */ |
||
| 44 | private $arrivalTime; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Time|null |
||
| 48 | */ |
||
| 49 | private $departureTime; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string|null |
||
| 53 | */ |
||
| 54 | private $endAddress; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Coordinate|null |
||
| 58 | */ |
||
| 59 | private $endLocation; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $startAddress; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var Coordinate|null |
||
| 68 | */ |
||
| 69 | private $startLocation; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var DirectionStep[] |
||
| 73 | */ |
||
| 74 | private $steps = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var DirectionWaypoint[] |
||
| 78 | */ |
||
| 79 | private $viaWaypoints = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 12 | public function hasDistance() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return Distance|null |
||
| 91 | */ |
||
| 92 | 276 | public function getDistance() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param Distance|null $distance |
||
| 99 | */ |
||
| 100 | 8 | public function setDistance(Distance $distance = null) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 12 | public function hasDuration() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return Duration|null |
||
| 115 | */ |
||
| 116 | 276 | public function getDuration() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param Duration|null $duration |
||
| 123 | */ |
||
| 124 | 8 | public function setDuration(Duration $duration = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | 12 | public function hasDurationInTraffic() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @return Duration|null |
||
| 139 | */ |
||
| 140 | 276 | public function getDurationInTraffic() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param Duration|null $durationInTraffic |
||
| 147 | */ |
||
| 148 | 8 | public function setDurationInTraffic(Duration $durationInTraffic = null) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 12 | public function hasArrivalTime() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return Time|null |
||
| 163 | */ |
||
| 164 | 276 | public function getArrivalTime() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param Time|null $arrivalTime |
||
| 171 | */ |
||
| 172 | 8 | public function setArrivalTime(Time $arrivalTime = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 12 | public function hasDepartureTime() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return Time|null |
||
| 187 | */ |
||
| 188 | 276 | public function getDepartureTime() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param Time|null $departureTime |
||
| 195 | */ |
||
| 196 | 8 | public function setDepartureTime(Time $departureTime = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | 12 | public function hasEndAddress() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @return string|null |
||
| 211 | */ |
||
| 212 | 276 | public function getEndAddress() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param string|null $endAddress |
||
| 219 | */ |
||
| 220 | 8 | public function setEndAddress($endAddress = null) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 12 | public function hasEndLocation() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @return Coordinate|null |
||
| 235 | */ |
||
| 236 | 276 | public function getEndLocation() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param Coordinate|null $endLocation |
||
| 243 | */ |
||
| 244 | 8 | public function setEndLocation(Coordinate $endLocation = null) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | 12 | public function hasStartAddress() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | 276 | public function getStartAddress() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param string|null $startAddress |
||
| 267 | */ |
||
| 268 | 8 | public function setStartAddress($startAddress = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | 12 | public function hasStartLocation() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return Coordinate|null |
||
| 283 | */ |
||
| 284 | 276 | public function getStartLocation() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param Coordinate|null $startLocation |
||
| 291 | */ |
||
| 292 | 8 | public function setStartLocation(Coordinate $startLocation = null) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | 20 | public function hasSteps() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @return DirectionStep[] |
||
| 307 | */ |
||
| 308 | 284 | public function getSteps() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param DirectionStep[] $steps |
||
| 315 | */ |
||
| 316 | 8 | public function setSteps(array $steps) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param DirectionStep[] $steps |
||
| 324 | */ |
||
| 325 | 8 | public function addSteps(array $steps) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param DirectionStep $step |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 16 | public function hasStep(DirectionStep $step) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param DirectionStep $step |
||
| 344 | */ |
||
| 345 | 16 | public function addStep(DirectionStep $step) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param DirectionStep $step |
||
| 354 | */ |
||
| 355 | 4 | public function removeStep(DirectionStep $step) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | 20 | public function hasViaWaypoints() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @return DirectionWaypoint[] |
||
| 371 | */ |
||
| 372 | 284 | public function getViaWaypoints() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param DirectionWaypoint[] $viaWaypoints |
||
| 379 | */ |
||
| 380 | 8 | public function setViaWaypoints(array $viaWaypoints) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param DirectionWaypoint[] $viaWaypoints |
||
| 388 | */ |
||
| 389 | 8 | public function addViaWaypoints(array $viaWaypoints) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param DirectionWaypoint $viaWaypoint |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | 16 | public function hasViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param DirectionWaypoint $viaWaypoint |
||
| 408 | */ |
||
| 409 | 16 | public function addViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param DirectionWaypoint $viaWaypoint |
||
| 418 | */ |
||
| 419 | 4 | public function removeViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 424 | } |
||
| 425 |