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 |
||
| 25 | final class Version implements JsonSerializable |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $major; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private $minor; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $patch; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var PreRelease |
||
| 44 | */ |
||
| 45 | private $preRelease; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Build |
||
| 49 | */ |
||
| 50 | private $build; |
||
| 51 | |||
| 52 | 81 | private function __construct($major, $minor, $patch, PreRelease $preRelease, Build $build) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param int $major |
||
| 63 | * @param int $minor |
||
| 64 | * @param int $patch |
||
| 65 | * @param PreRelease|array|string $preRelease |
||
| 66 | * @param Build|array|string $build |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | 85 | public static function fromAllElements($major, $minor, $patch, $preRelease, $build) |
|
| 87 | |||
| 88 | 85 | private static function validateVersionElement($element, $value) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param int $major |
||
| 97 | * @return self |
||
| 98 | */ |
||
| 99 | 10 | public static function fromMajor($major) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param int $major |
||
| 106 | * @param int $minor |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | 2 | public static function fromMinor($major, $minor) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param int $major |
||
| 116 | * @param int $minor |
||
| 117 | * @param int $patch |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | 2 | public static function fromPatch($major, $minor, $patch) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param int $major |
||
| 127 | * @param int $minor |
||
| 128 | * @param int $patch |
||
| 129 | * @param @param PreRelease|array|string $preRelease |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | 1 | public static function fromPreRelease($major, $minor, $patch, $preRelease) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param int $major |
||
| 139 | * @param int $minor |
||
| 140 | * @param int $patch |
||
| 141 | * @param Build|array|string $build |
||
| 142 | * @return self |
||
| 143 | */ |
||
| 144 | 1 | public static function fromBuild($major, $minor, $patch, $build) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $versionString |
||
| 151 | * @return self |
||
| 152 | * @throws InvalidVersionStringException |
||
| 153 | */ |
||
| 154 | 78 | public static function fromString($versionString) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | 18 | public function getMajor() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | 18 | public function getMinor() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | 18 | public function getPatch() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @return PreRelease |
||
| 208 | */ |
||
| 209 | 9 | public function getPreRelease() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return Build |
||
| 216 | */ |
||
| 217 | 7 | public function getBuild() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | 58 | public function isPreRelease() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 42 | public function isBuild() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param self|string $version |
||
| 240 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 241 | */ |
||
| 242 | 41 | public function compareTo($version) |
|
| 243 | { |
||
| 244 | 41 | if (!$version instanceof self) { |
|
| 245 | 29 | $version = self::fromString((string) $version); |
|
|
1 ignored issue
–
show
|
|||
| 246 | 29 | } |
|
| 247 | |||
| 248 | 41 | if ($this->major > $version->major) { |
|
| 249 | 4 | return 1; |
|
| 250 | } |
||
| 251 | |||
| 252 | 40 | if ($this->major < $version->major) { |
|
| 253 | 7 | return -1; |
|
| 254 | } |
||
| 255 | |||
| 256 | 37 | if ($this->minor > $version->minor) { |
|
| 257 | 6 | return 1; |
|
| 258 | } |
||
| 259 | |||
| 260 | 33 | if ($this->minor < $version->minor) { |
|
| 261 | 1 | return -1; |
|
| 262 | } |
||
| 263 | |||
| 264 | 33 | if ($this->patch > $version->patch) { |
|
| 265 | 4 | return 1; |
|
| 266 | } |
||
| 267 | |||
| 268 | 30 | if ($this->patch < $version->patch) { |
|
| 269 | return -1; |
||
| 270 | } |
||
| 271 | |||
| 272 | 30 | if (!$this->isPreRelease() && $version->isPreRelease()) { |
|
| 273 | // normal version has greater precedence than a pre-release version version |
||
| 274 | 4 | return 1; |
|
| 275 | } |
||
| 276 | |||
| 277 | 26 | if ($this->isPreRelease() && !$version->isPreRelease()) { |
|
| 278 | // pre-release version has lower precedence than a normal version |
||
| 279 | 1 | return -1; |
|
| 280 | } |
||
| 281 | |||
| 282 | 25 | if ($this->isPreRelease() && $version->isPreRelease()) { |
|
| 283 | 16 | $result = $this->preRelease->compareTo($version->preRelease); |
|
| 284 | |||
| 285 | 16 | if ($result > 0) { |
|
| 286 | 12 | return 1; |
|
| 287 | 4 | } elseif ($result < 0) { |
|
| 288 | 2 | return -1; |
|
| 289 | } |
||
| 290 | 2 | } |
|
| 291 | |||
| 292 | 11 | return 0; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param self|string $version |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 5 | public function isEqualTo($version) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param self|string $version |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | 12 | public function isGreaterThan($version) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param self|string $version |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 3 | public function isGreaterOrEqualTo($version) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param self|string $version |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | 4 | public function isLessThan($version) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param self|string $version |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | 3 | public function isLessOrEqualTo($version) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param ConstraintInterface|string $constraint |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | 2 | public function matches($constraint) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @return self |
||
| 355 | */ |
||
| 356 | 1 | public function withMajorIncremented() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @return self |
||
| 363 | */ |
||
| 364 | 2 | public function withMinorIncremented() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @return self |
||
| 371 | */ |
||
| 372 | 1 | public function withPatchIncremented() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param PreRelease|array|string $preRelease |
||
| 379 | * @return self |
||
| 380 | */ |
||
| 381 | 2 | public function withPreRelease($preRelease) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param Build|array|string $build |
||
| 388 | * @return self |
||
| 389 | */ |
||
| 390 | 1 | public function withBuild($build) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 28 | public function getVersionString() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 24 | public function __toString() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | 4 | public function jsonSerialize() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | 4 | public function toArray() |
|
| 438 | } |
||
| 439 |