Complex classes like Version 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 Version, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class Version implements JsonSerializable |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $major; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | private $minor; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $patch; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var PreRelease |
||
| 46 | */ |
||
| 47 | private $preRelease; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Build |
||
| 51 | */ |
||
| 52 | private $build; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ComparatorInterface |
||
| 56 | */ |
||
| 57 | private static $comparator; |
||
| 58 | |||
| 59 | 77 | private function __construct($major, $minor, $patch, PreRelease $preRelease, Build $build) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param int $major |
||
| 70 | * @param int $minor |
||
| 71 | * @param int $patch |
||
| 72 | * @param PreRelease|array|string $preRelease |
||
| 73 | * @param Build|array|string $build |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | 81 | public static function fromAllElements($major, $minor, $patch, $preRelease, $build) |
|
| 94 | |||
| 95 | 81 | private static function validateVersionElement($element, $value) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param int $major |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | 12 | public static function fromMajor($major) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param int $major |
||
| 113 | * @param int $minor |
||
| 114 | * @return self |
||
| 115 | */ |
||
| 116 | 2 | public static function fromMinor($major, $minor) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param int $major |
||
| 123 | * @param int $minor |
||
| 124 | * @param int $patch |
||
| 125 | * @return self |
||
| 126 | */ |
||
| 127 | 2 | public static function fromPatch($major, $minor, $patch) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param int $major |
||
| 134 | * @param int $minor |
||
| 135 | * @param int $patch |
||
| 136 | * @param @param PreRelease|array|string $preRelease |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | 1 | public static function fromPreRelease($major, $minor, $patch, $preRelease) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param int $major |
||
| 146 | * @param int $minor |
||
| 147 | * @param int $patch |
||
| 148 | * @param Build|array|string $build |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | 1 | public static function fromBuild($major, $minor, $patch, $build) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $versionString |
||
| 158 | * @return self |
||
| 159 | * @throws InvalidVersionStringException |
||
| 160 | */ |
||
| 161 | 74 | public static function fromString($versionString) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @return int |
||
| 191 | */ |
||
| 192 | 55 | public function getMajor() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | 51 | public function getMinor() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @return int |
||
| 207 | */ |
||
| 208 | 45 | public function getPatch() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return PreRelease |
||
| 215 | */ |
||
| 216 | 28 | public function getPreRelease() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return Build |
||
| 223 | */ |
||
| 224 | 7 | public function getBuild() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 49 | public function isPreRelease() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | 30 | public function isBuild() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param self|string $version |
||
| 247 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 248 | */ |
||
| 249 | 22 | public function compareTo($version) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param ComparatorInterface $comparator |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | 1 | public static function setComparator(ComparatorInterface $comparator) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @return ComparatorInterface |
||
| 269 | */ |
||
| 270 | 22 | public static function getComparator() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param self|string $version |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 5 | public function isEqualTo($version) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param self|string $version |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | 1 | public function isNotEqualTo($version) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param self|string $version |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 3 | public function isGreaterThan($version) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param self|string $version |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | 3 | public function isGreaterOrEqualTo($version) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param self|string $version |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | 3 | public function isLessThan($version) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param self|string $version |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | 3 | public function isLessOrEqualTo($version) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param ConstraintInterface|string $constraint |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 2 | public function matches($constraint) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @return self |
||
| 348 | */ |
||
| 349 | 1 | public function withMajorIncremented() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @return self |
||
| 356 | */ |
||
| 357 | 2 | public function withMinorIncremented() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @return self |
||
| 364 | */ |
||
| 365 | 1 | public function withPatchIncremented() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param PreRelease|array|string $preRelease |
||
| 372 | * @return self |
||
| 373 | */ |
||
| 374 | 2 | public function withPreRelease($preRelease) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @param Build|array|string $build |
||
| 381 | * @return self |
||
| 382 | */ |
||
| 383 | 1 | public function withBuild($build) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 16 | public function getVersionString() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | 12 | public function __toString() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | 4 | public function jsonSerialize() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | 4 | public function toArray() |
|
| 431 | } |
||
| 432 |