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