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 | 12 | public function getBound() |
|
| 80 | |||
| 81 | 8 | public function setBound(Bound $bound = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | 12 | public function hasCopyrights() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return string|null |
||
| 96 | */ |
||
| 97 | 12 | public function getCopyrights() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param string|null $copyrights |
||
| 104 | */ |
||
| 105 | 8 | public function setCopyrights($copyrights = null) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | 20 | public function hasLegs() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return DirectionLeg[] |
||
| 120 | */ |
||
| 121 | 20 | public function getLegs() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param DirectionLeg[] $legs |
||
| 128 | */ |
||
| 129 | 8 | public function setLegs(array $legs) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param DirectionLeg[] $legs |
||
| 137 | */ |
||
| 138 | 8 | public function addLegs(array $legs) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 16 | public function hasLeg(DirectionLeg $leg) |
|
| 152 | |||
| 153 | 16 | public function addLeg(DirectionLeg $leg) |
|
| 159 | |||
| 160 | 4 | public function removeLeg(DirectionLeg $leg) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | 12 | public function hasOverviewPolyline() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return EncodedPolyline|null |
||
| 176 | */ |
||
| 177 | 12 | public function getOverviewPolyline() |
|
| 181 | |||
| 182 | 8 | public function setOverviewPolyline(EncodedPolyline $overviewPolyline = null) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 12 | public function hasSummary() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return string|null |
||
| 197 | */ |
||
| 198 | 12 | public function getSummary() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param string|null $summary |
||
| 205 | */ |
||
| 206 | 8 | public function setSummary($summary = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 12 | public function hasFare() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return Fare|null |
||
| 221 | */ |
||
| 222 | 12 | public function getFare() |
|
| 226 | |||
| 227 | 8 | public function setFare(Fare $fare = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 20 | public function hasWarnings() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return string[] |
||
| 242 | */ |
||
| 243 | 20 | public function getWarnings() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param string[] $warnings |
||
| 250 | */ |
||
| 251 | 8 | public function setWarnings(array $warnings) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param string[] $warnings |
||
| 259 | */ |
||
| 260 | 8 | public function addWarnings(array $warnings) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param $warning |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 16 | public function hasWarning($warning) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $warning |
||
| 279 | */ |
||
| 280 | 16 | public function addWarning($warning) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $warning |
||
| 289 | */ |
||
| 290 | 4 | public function removeWarning($warning) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 12 | public function hasWaypointOrders() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return int[] |
||
| 306 | */ |
||
| 307 | 12 | public function getWaypointOrders() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param int[] $waypointOrders |
||
| 314 | */ |
||
| 315 | 4 | public function setWaypointOrders(array $waypointOrders) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param int[] $waypointOrders |
||
| 323 | */ |
||
| 324 | 4 | public function addWaypointOrders(array $waypointOrders) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param $waypointOrder |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | 8 | public function hasWaypointOrder($waypointOrder) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param int $waypointOrder |
||
| 345 | */ |
||
| 346 | 8 | public function addWaypointOrder($waypointOrder) |
|
| 350 | } |
||
| 351 |