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 |
||
| 22 | class DirectionsRoute |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Bound|null |
||
| 26 | */ |
||
| 27 | private $bound; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | private $copyrights; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var DirectionsLeg[] |
||
| 36 | */ |
||
| 37 | private $legs = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var EncodedPolyline|null |
||
| 41 | */ |
||
| 42 | private $overviewPolyline; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | private $summary; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var DirectionsFare|null |
||
| 51 | */ |
||
| 52 | private $fare; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string[] |
||
| 56 | */ |
||
| 57 | private $warnings = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int[] |
||
| 61 | */ |
||
| 62 | private $waypointOrders = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public function hasBound() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return Bound|null |
||
| 74 | */ |
||
| 75 | public function getBound() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param Bound|null $bound |
||
| 82 | */ |
||
| 83 | public function setBound(Bound $bound = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public function hasCopyrights() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string|null |
||
| 98 | */ |
||
| 99 | public function getCopyrights() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string|null $copyrights |
||
| 106 | */ |
||
| 107 | public function setCopyrights($copyrights = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function hasLegs() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return DirectionsLeg[] |
||
| 122 | */ |
||
| 123 | public function getLegs() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param DirectionsLeg[] $legs |
||
| 130 | */ |
||
| 131 | public function setLegs(array $legs) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param DirectionsLeg[] $legs |
||
| 139 | */ |
||
| 140 | public function addLegs(array $legs) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param DirectionsLeg $leg |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function hasLeg(DirectionsLeg $leg) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param DirectionsLeg $leg |
||
| 159 | */ |
||
| 160 | public function addLeg(DirectionsLeg $leg) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param DirectionsLeg $leg |
||
| 169 | */ |
||
| 170 | public function removeLeg(DirectionsLeg $leg) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function hasOverviewPolyline() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return EncodedPolyline|null |
||
| 186 | */ |
||
| 187 | public function getOverviewPolyline() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param EncodedPolyline|null $overviewPolyline |
||
| 194 | */ |
||
| 195 | public function setOverviewPolyline(EncodedPolyline $overviewPolyline = null) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function hasSummary() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string|null |
||
| 210 | */ |
||
| 211 | public function getSummary() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string|null $summary |
||
| 218 | */ |
||
| 219 | public function setSummary($summary = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function hasFare() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return DirectionsFare|null |
||
| 234 | */ |
||
| 235 | public function getFare() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param DirectionsFare|null $fare |
||
| 242 | */ |
||
| 243 | public function setFare(DirectionsFare $fare = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function hasWarnings() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string[] |
||
| 258 | */ |
||
| 259 | public function getWarnings() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string[] $warnings |
||
| 266 | */ |
||
| 267 | public function setWarnings(array $warnings) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string[] $warnings |
||
| 275 | */ |
||
| 276 | public function addWarnings(array $warnings) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param $warning |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function hasWarning($warning) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $warning |
||
| 295 | */ |
||
| 296 | public function addWarning($warning) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $warning |
||
| 305 | */ |
||
| 306 | public function removeWarning($warning) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function hasWaypointOrders() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return int[] |
||
| 321 | */ |
||
| 322 | public function getWaypointOrders() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param int[] $waypointOrders |
||
| 329 | */ |
||
| 330 | public function setWaypointOrders(array $waypointOrders) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param int[] $waypointOrders |
||
| 338 | */ |
||
| 339 | public function addWaypointOrders(array $waypointOrders) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $waypointOrder |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function hasWaypointOrder($waypointOrder) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param int $waypointOrder |
||
| 360 | */ |
||
| 361 | public function addWaypointOrder($waypointOrder) |
||
| 365 | } |
||
| 366 |