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 |
||
| 16 | class Version implements JsonSerializable |
||
| 17 | { |
||
| 18 | protected $major; |
||
| 19 | protected $minor; |
||
| 20 | protected $patch; |
||
| 21 | protected $preRelease; |
||
| 22 | protected $build; |
||
| 23 | |||
| 24 | protected static $comparator; |
||
| 25 | |||
| 26 | 79 | protected function __construct(int $major, int $minor, int $patch, ?PreRelease $preRelease, ?Build $build) |
|
| 38 | |||
| 39 | 79 | public static function from(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null): Version |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @throws InvalidVersionString |
||
| 46 | */ |
||
| 47 | 77 | public static function fromString(string $versionString): Version |
|
| 48 | { |
||
| 49 | 77 | if (!preg_match( |
|
| 50 | '#^' |
||
| 51 | . '(v|release\-)?' |
||
| 52 | . '(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)' |
||
| 53 | . '(?:\-(?P<preRelease>(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z\-][0-9a-zA-Z\-]*))*))?' |
||
| 54 | . '(?:\+(?P<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?' |
||
| 55 | 77 | . '$#', |
|
| 56 | 77 | $versionString, |
|
| 57 | 77 | $parts |
|
| 58 | )) { |
||
| 59 | 4 | throw InvalidVersionString::notParsable($versionString); |
|
| 60 | } |
||
| 61 | |||
| 62 | 73 | return static::from( |
|
| 63 | 73 | (int) $parts['major'], |
|
| 64 | 73 | (int) $parts['minor'], |
|
| 65 | 73 | (int) $parts['patch'], |
|
| 66 | 73 | (isset($parts['preRelease']) && '' !== $parts['preRelease']) ? PreRelease::fromString($parts['preRelease']) : null, |
|
| 67 | 73 | (isset($parts['build']) && '' !== $parts['build']) ? Build::fromString($parts['build']) : null |
|
| 68 | ); |
||
| 69 | } |
||
| 70 | |||
| 71 | 57 | public function getMajor(): int |
|
| 75 | |||
| 76 | 53 | public function getMinor(): int |
|
| 80 | |||
| 81 | 49 | public function getPatch(): int |
|
| 85 | |||
| 86 | 18 | public function getPreRelease(): ?PreRelease |
|
| 90 | |||
| 91 | 6 | public function getBuild(): ?Build |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param Version|string $version |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 6 | public function isEqualTo($version): bool |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param Version|string $version |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 1 | public function isNotEqualTo($version): bool |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param Version|string $version |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | 3 | public function isGreaterThan($version): bool |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param Version|string $version |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 4 | public function isGreaterOrEqualTo($version): bool |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param Version|string $version |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | 3 | public function isLessThan($version): bool |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param Version|string $version |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | 2 | public function isLessOrEqualTo($version): bool |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param Version|string $version |
||
| 152 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 153 | */ |
||
| 154 | 23 | public function compareTo($version): int |
|
| 162 | |||
| 163 | 2 | public function isMajorRelease(): bool |
|
| 167 | |||
| 168 | 1 | public function isMinorRelease(): bool |
|
| 172 | |||
| 173 | 1 | public function isPatchRelease(): bool |
|
| 177 | |||
| 178 | 63 | public function isPreRelease(): bool |
|
| 182 | |||
| 183 | 42 | public function hasBuild(): bool |
|
| 187 | |||
| 188 | 1 | public function incrementMajor(): Version |
|
| 192 | |||
| 193 | 2 | public function incrementMinor(): Version |
|
| 197 | |||
| 198 | 1 | public function incrementPatch(): Version |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param PreRelease|string|null $preRelease |
||
| 205 | * @return Version |
||
| 206 | */ |
||
| 207 | 2 | public function withPreRelease($preRelease): Version |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param Build|string|null $build |
||
| 218 | * @return Version |
||
| 219 | */ |
||
| 220 | 1 | public function withBuild($build): Version |
|
| 228 | |||
| 229 | 2 | public function matches(Constraint $constraint): bool |
|
| 233 | |||
| 234 | 14 | public function toString(): string |
|
| 244 | |||
| 245 | public function __toString(): string |
||
| 249 | |||
| 250 | 4 | public function jsonSerialize(): string |
|
| 254 | |||
| 255 | 4 | public function toArray(): array |
|
| 265 | |||
| 266 | 1 | public static function setComparator(?Comparator $comparator): void |
|
| 270 | |||
| 271 | 23 | protected function getComparator(): Comparator |
|
| 279 | } |
||
| 280 |