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 | public const REGEX = '#^(v|release\-)?(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:\-(?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\-]*))*))?(?:\+(?P<build>[0-9a-zA-Z\-]+(?:\.[0-9a-zA-Z\-]+)*))?$#'; |
||
| 19 | |||
| 20 | protected $major; |
||
| 21 | protected $minor; |
||
| 22 | protected $patch; |
||
| 23 | protected $preRelease; |
||
| 24 | protected $build; |
||
| 25 | |||
| 26 | protected static $comparator; |
||
| 27 | |||
| 28 | 79 | final protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease = null, Build $build = null) |
|
| 40 | |||
| 41 | 28 | public static function from(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @throws InvalidVersionString |
||
| 48 | */ |
||
| 49 | 77 | public static function fromString(string $versionString) |
|
| 63 | |||
| 64 | 57 | public function getMajor(): int |
|
| 68 | |||
| 69 | 53 | public function getMinor(): int |
|
| 73 | |||
| 74 | 49 | public function getPatch(): int |
|
| 78 | |||
| 79 | 18 | public function getPreRelease(): ?PreRelease |
|
| 83 | |||
| 84 | 6 | public function getBuild(): ?Build |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param Version|string $version |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 6 | public function isEqualTo($version): bool |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param Version|string $version |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 1 | public function isNotEqualTo($version): bool |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param Version|string $version |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 3 | public function isGreaterThan($version): bool |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param Version|string $version |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | 4 | public function isGreaterOrEqualTo($version): bool |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param Version|string $version |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 3 | public function isLessThan($version): bool |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param Version|string $version |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 2 | public function isLessOrEqualTo($version): bool |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param Version|string $version |
||
| 145 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 146 | */ |
||
| 147 | 23 | public function compareTo($version): int |
|
| 155 | |||
| 156 | 2 | public function isMajorRelease(): bool |
|
| 160 | |||
| 161 | 1 | public function isMinorRelease(): bool |
|
| 165 | |||
| 166 | 1 | public function isPatchRelease(): bool |
|
| 170 | |||
| 171 | 63 | public function isPreRelease(): bool |
|
| 175 | |||
| 176 | 42 | public function hasBuild(): bool |
|
| 180 | |||
| 181 | 1 | public function incrementMajor(): Version |
|
| 185 | |||
| 186 | 2 | public function incrementMinor(): Version |
|
| 190 | |||
| 191 | 1 | public function incrementPatch(): Version |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param PreRelease|string|null $preRelease |
||
| 198 | * @return Version |
||
| 199 | */ |
||
| 200 | 2 | public function withPreRelease($preRelease): Version |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param Build|string|null $build |
||
| 211 | * @return Version |
||
| 212 | */ |
||
| 213 | 1 | public function withBuild($build): Version |
|
| 221 | |||
| 222 | 2 | public function matches(Constraint $constraint): bool |
|
| 226 | |||
| 227 | 14 | public function toString(): string |
|
| 237 | |||
| 238 | public function __toString(): string |
||
| 242 | |||
| 243 | 4 | public function jsonSerialize(): string |
|
| 247 | |||
| 248 | 4 | public function toArray(): array |
|
| 258 | |||
| 259 | 1 | public static function setComparator(?Comparator $comparator): void |
|
| 263 | |||
| 264 | 23 | protected function getComparator(): Comparator |
|
| 272 | } |
||
| 273 |