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 |
||
| 18 | class Version implements JsonSerializable |
||
| 19 | { |
||
| 20 | /** @var int */ |
||
| 21 | protected $major; |
||
| 22 | |||
| 23 | /** @var int */ |
||
| 24 | protected $minor; |
||
| 25 | |||
| 26 | /** @var int */ |
||
| 27 | protected $patch; |
||
| 28 | |||
| 29 | /** @var PreRelease */ |
||
| 30 | protected $preRelease; |
||
| 31 | |||
| 32 | /** @var Build */ |
||
| 33 | protected $build; |
||
| 34 | |||
| 35 | /** @var Comparator|null */ |
||
| 36 | protected static $comparator; |
||
| 37 | |||
| 38 | 82 | protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build) |
|
| 50 | |||
| 51 | 82 | public static function fromParts(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null): Version |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $versionString |
||
| 58 | * |
||
| 59 | * @return Version |
||
| 60 | * @throws InvalidVersionString |
||
| 61 | * |
||
| 62 | */ |
||
| 63 | 80 | public static function fromString(string $versionString): Version |
|
| 84 | |||
| 85 | 60 | public function getMajor(): int |
|
| 89 | |||
| 90 | 55 | public function getMinor(): int |
|
| 94 | |||
| 95 | 51 | public function getPatch(): int |
|
| 99 | |||
| 100 | 48 | public function getPreRelease(): PreRelease |
|
| 104 | |||
| 105 | 28 | public function getBuild(): Build |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @param Version|string $version |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 6 | public function isEqualTo($version): bool |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param Version|string $version |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 1 | public function isNotEqualTo($version): bool |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param Version|string $version |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | 3 | public function isGreaterThan($version): bool |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param Version|string $version |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | 6 | public function isGreaterOrEqualTo($version): bool |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param Version|string $version |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 3 | public function isLessThan($version): bool |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param Version|string $version |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | 2 | public function isLessOrEqualTo($version): bool |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param Version|string $version |
||
| 166 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 167 | */ |
||
| 168 | 25 | public function compareTo($version): int |
|
| 176 | |||
| 177 | 2 | public function isMajorRelease(): bool |
|
| 181 | |||
| 182 | 1 | public function isMinorRelease(): bool |
|
| 186 | |||
| 187 | 1 | public function isPatchRelease(): bool |
|
| 191 | |||
| 192 | 38 | public function isPreRelease(): bool |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @deprecated Use hasBuild() instead |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | 1 | public function isBuild(): bool |
|
| 205 | |||
| 206 | 15 | public function hasBuild(): bool |
|
| 210 | |||
| 211 | 1 | public function incrementMajor(): Version |
|
| 215 | |||
| 216 | 2 | public function incrementMinor(): Version |
|
| 220 | |||
| 221 | 1 | public function incrementPatch(): Version |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param PreRelease|string|null $preRelease |
||
| 228 | * @return Version |
||
| 229 | */ |
||
| 230 | 2 | public function withPreRelease($preRelease): Version |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param Build|string|null $build |
||
| 241 | * @return Version |
||
| 242 | */ |
||
| 243 | 1 | public function withBuild($build): Version |
|
| 251 | |||
| 252 | 4 | public function matches(Constraint $constraint): bool |
|
| 256 | |||
| 257 | 14 | public function toString(): string |
|
| 267 | |||
| 268 | 10 | public function __toString(): string |
|
| 272 | |||
| 273 | 4 | public function jsonSerialize(): string |
|
| 277 | |||
| 278 | 4 | public function toArray(): array |
|
| 288 | |||
| 289 | 1 | public static function setComparator(?Comparator $comparator): void |
|
| 293 | |||
| 294 | 25 | protected function getComparator(): Comparator |
|
| 302 | } |
||
| 303 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.