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 | 12 | public function getDistance() |
|
| 96 | |||
| 97 | 8 | public function setDistance(Distance $distance = null) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 12 | public function hasDuration() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return Duration|null |
||
| 112 | */ |
||
| 113 | 12 | public function getDuration() |
|
| 117 | |||
| 118 | 8 | public function setDuration(Duration $duration = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 12 | public function hasDurationInTraffic() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return Duration|null |
||
| 133 | */ |
||
| 134 | 12 | public function getDurationInTraffic() |
|
| 138 | |||
| 139 | 8 | public function setDurationInTraffic(Duration $durationInTraffic = null) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 12 | public function hasArrivalTime() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return Time|null |
||
| 154 | */ |
||
| 155 | 12 | public function getArrivalTime() |
|
| 159 | |||
| 160 | 8 | public function setArrivalTime(Time $arrivalTime = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 12 | public function hasDepartureTime() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @return Time|null |
||
| 175 | */ |
||
| 176 | 12 | public function getDepartureTime() |
|
| 180 | |||
| 181 | 8 | public function setDepartureTime(Time $departureTime = null) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | 12 | public function hasEndAddress() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return string|null |
||
| 196 | */ |
||
| 197 | 12 | public function getEndAddress() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param string|null $endAddress |
||
| 204 | */ |
||
| 205 | 8 | public function setEndAddress($endAddress = null) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 12 | public function hasEndLocation() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @return Coordinate|null |
||
| 220 | */ |
||
| 221 | 12 | public function getEndLocation() |
|
| 225 | |||
| 226 | 8 | public function setEndLocation(Coordinate $endLocation = null) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | 12 | public function hasStartAddress() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @return string|null |
||
| 241 | */ |
||
| 242 | 12 | public function getStartAddress() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string|null $startAddress |
||
| 249 | */ |
||
| 250 | 8 | public function setStartAddress($startAddress = null) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 12 | public function hasStartLocation() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return Coordinate|null |
||
| 265 | */ |
||
| 266 | 12 | public function getStartLocation() |
|
| 270 | |||
| 271 | 8 | public function setStartLocation(Coordinate $startLocation = null) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 20 | public function hasSteps() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @return DirectionStep[] |
||
| 286 | */ |
||
| 287 | 20 | public function getSteps() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param DirectionStep[] $steps |
||
| 294 | */ |
||
| 295 | 8 | public function setSteps(array $steps) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param DirectionStep[] $steps |
||
| 303 | */ |
||
| 304 | 8 | public function addSteps(array $steps) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 16 | public function hasStep(DirectionStep $step) |
|
| 318 | |||
| 319 | 16 | public function addStep(DirectionStep $step) |
|
| 325 | |||
| 326 | 4 | public function removeStep(DirectionStep $step) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | 20 | public function hasViaWaypoints() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return DirectionWaypoint[] |
||
| 342 | */ |
||
| 343 | 20 | public function getViaWaypoints() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param DirectionWaypoint[] $viaWaypoints |
||
| 350 | */ |
||
| 351 | 8 | public function setViaWaypoints(array $viaWaypoints) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param DirectionWaypoint[] $viaWaypoints |
||
| 359 | */ |
||
| 360 | 8 | public function addViaWaypoints(array $viaWaypoints) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | 16 | public function hasViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 374 | |||
| 375 | 16 | public function addViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 381 | |||
| 382 | 4 | public function removeViaWaypoint(DirectionWaypoint $viaWaypoint) |
|
| 387 | } |
||
| 388 |