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 | /** @var int */ |
||
| 19 | protected $major; |
||
| 20 | |||
| 21 | /** @var int */ |
||
| 22 | protected $minor; |
||
| 23 | |||
| 24 | /** @var int */ |
||
| 25 | protected $patch; |
||
| 26 | |||
| 27 | /** @var PreRelease */ |
||
| 28 | protected $preRelease; |
||
| 29 | |||
| 30 | /** @var Build */ |
||
| 31 | protected $build; |
||
| 32 | |||
| 33 | /** @var Comparator|null */ |
||
| 34 | protected static $comparator; |
||
| 35 | |||
| 36 | 78 | protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build) |
|
| 48 | |||
| 49 | 78 | public static function from(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null): Version |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $versionString |
||
| 56 | * |
||
| 57 | * @return Version |
||
| 58 | * @throws InvalidVersionString |
||
| 59 | * |
||
| 60 | */ |
||
| 61 | 76 | public static function fromString(string $versionString): Version |
|
| 82 | |||
| 83 | 57 | public function getMajor(): int |
|
| 87 | |||
| 88 | 53 | public function getMinor(): int |
|
| 92 | |||
| 93 | 49 | public function getPatch(): int |
|
| 97 | |||
| 98 | 46 | public function getPreRelease(): PreRelease |
|
| 102 | |||
| 103 | 26 | public function getBuild(): Build |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param Version|string $version |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 6 | public function isEqualTo($version): bool |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param Version|string $version |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 1 | public function isNotEqualTo($version): bool |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param Version|string $version |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | 3 | public function isGreaterThan($version): bool |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param Version|string $version |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 4 | public function isGreaterOrEqualTo($version): bool |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param Version|string $version |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 3 | public function isLessThan($version): bool |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param Version|string $version |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 2 | public function isLessOrEqualTo($version): bool |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param Version|string $version |
||
| 164 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 165 | */ |
||
| 166 | 23 | public function compareTo($version): int |
|
| 174 | |||
| 175 | 2 | public function isMajorRelease(): bool |
|
| 179 | |||
| 180 | 1 | public function isMinorRelease(): bool |
|
| 184 | |||
| 185 | 1 | public function isPatchRelease(): bool |
|
| 189 | |||
| 190 | 37 | public function isPreRelease(): bool |
|
| 194 | |||
| 195 | 15 | public function hasBuild(): bool |
|
| 199 | |||
| 200 | 1 | public function incrementMajor(): Version |
|
| 204 | |||
| 205 | 2 | public function incrementMinor(): Version |
|
| 209 | |||
| 210 | 1 | public function incrementPatch(): Version |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param PreRelease|string|null $preRelease |
||
| 217 | * @return Version |
||
| 218 | */ |
||
| 219 | 2 | public function withPreRelease($preRelease): Version |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param Build|string|null $build |
||
| 230 | * @return Version |
||
| 231 | */ |
||
| 232 | 1 | public function withBuild($build): Version |
|
| 240 | |||
| 241 | 2 | public function matches(Constraint $constraint): bool |
|
| 245 | |||
| 246 | 14 | public function toString(): string |
|
| 256 | |||
| 257 | public function __toString(): string |
||
| 258 | { |
||
| 259 | return $this->toString(); |
||
| 260 | } |
||
| 261 | |||
| 262 | 4 | public function jsonSerialize(): string |
|
| 266 | |||
| 267 | 4 | public function toArray(): array |
|
| 277 | |||
| 278 | 1 | public static function setComparator(?Comparator $comparator): void |
|
| 282 | |||
| 283 | 23 | protected function getComparator(): Comparator |
|
| 291 | } |
||
| 292 |
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.