Complex classes like DirectionsRoute 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 DirectionsRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DirectionsRoute |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Bound|null |
||
| 27 | */ |
||
| 28 | private $bound; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $copyrights; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var DirectionsLeg[] |
||
| 37 | */ |
||
| 38 | private $legs = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EncodedPolyline|null |
||
| 42 | */ |
||
| 43 | private $overviewPolyline; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string|null |
||
| 47 | */ |
||
| 48 | private $summary; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Fare|null |
||
| 52 | */ |
||
| 53 | private $fare; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | private $warnings = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int[] |
||
| 62 | */ |
||
| 63 | private $waypointOrders = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public function hasBound() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return Bound|null |
||
| 75 | */ |
||
| 76 | public function getBound() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Bound|null $bound |
||
| 83 | */ |
||
| 84 | public function setBound(Bound $bound = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function hasCopyrights() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string|null |
||
| 99 | */ |
||
| 100 | public function getCopyrights() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string|null $copyrights |
||
| 107 | */ |
||
| 108 | public function setCopyrights($copyrights = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function hasLegs() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return DirectionsLeg[] |
||
| 123 | */ |
||
| 124 | public function getLegs() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param DirectionsLeg[] $legs |
||
| 131 | */ |
||
| 132 | public function setLegs(array $legs) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param DirectionsLeg[] $legs |
||
| 140 | */ |
||
| 141 | public function addLegs(array $legs) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param DirectionsLeg $leg |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function hasLeg(DirectionsLeg $leg) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param DirectionsLeg $leg |
||
| 160 | */ |
||
| 161 | public function addLeg(DirectionsLeg $leg) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param DirectionsLeg $leg |
||
| 170 | */ |
||
| 171 | public function removeLeg(DirectionsLeg $leg) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasOverviewPolyline() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return EncodedPolyline|null |
||
| 187 | */ |
||
| 188 | public function getOverviewPolyline() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param EncodedPolyline|null $overviewPolyline |
||
| 195 | */ |
||
| 196 | public function setOverviewPolyline(EncodedPolyline $overviewPolyline = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function hasSummary() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string|null |
||
| 211 | */ |
||
| 212 | public function getSummary() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string|null $summary |
||
| 219 | */ |
||
| 220 | public function setSummary($summary = null) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function hasFare() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return Fare|null |
||
| 235 | */ |
||
| 236 | public function getFare() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param Fare|null $fare |
||
| 243 | */ |
||
| 244 | public function setFare(Fare $fare = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function hasWarnings() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string[] |
||
| 259 | */ |
||
| 260 | public function getWarnings() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string[] $warnings |
||
| 267 | */ |
||
| 268 | public function setWarnings(array $warnings) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string[] $warnings |
||
| 276 | */ |
||
| 277 | public function addWarnings(array $warnings) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $warning |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function hasWarning($warning) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $warning |
||
| 296 | */ |
||
| 297 | public function addWarning($warning) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $warning |
||
| 306 | */ |
||
| 307 | public function removeWarning($warning) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function hasWaypointOrders() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return int[] |
||
| 322 | */ |
||
| 323 | public function getWaypointOrders() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param int[] $waypointOrders |
||
| 330 | */ |
||
| 331 | public function setWaypointOrders(array $waypointOrders) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param int[] $waypointOrders |
||
| 339 | */ |
||
| 340 | public function addWaypointOrders(array $waypointOrders) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param $waypointOrder |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function hasWaypointOrder($waypointOrder) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param int $waypointOrder |
||
| 361 | */ |
||
| 362 | public function addWaypointOrder($waypointOrder) |
||
| 366 | } |
||
| 367 |