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 |
||
| 20 | class Version implements JsonSerializable |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | protected $major; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $minor; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected $patch; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var PreRelease |
||
| 39 | */ |
||
| 40 | protected $preRelease; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Build |
||
| 44 | */ |
||
| 45 | protected $build; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var ComparatorInterface |
||
| 49 | */ |
||
| 50 | protected static $comparator; |
||
| 51 | |||
| 52 | 74 | protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build) |
|
| 60 | |||
| 61 | 77 | public static function fromParts(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build) : Version |
|
| 69 | |||
| 70 | 10 | public static function fromMajor(int $major) : Version |
|
| 74 | |||
| 75 | 2 | public static function fromMinor(int $major, int $minor) : Version |
|
| 79 | |||
| 80 | 2 | public static function fromPatch(int $major, int $minor, int $patch) : Version |
|
| 84 | |||
| 85 | 1 | public static function fromPreRelease(int $major, int $minor, int $patch, PreRelease $preRelease) : Version |
|
| 89 | |||
| 90 | 1 | public static function fromBuild(int $major, int $minor, int $patch, Build $build) : Version |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $versionString |
||
| 97 | * @return Version |
||
| 98 | * @throws InvalidVersionStringException |
||
| 99 | */ |
||
| 100 | 71 | public static function fromString(string $versionString) : Version |
|
| 127 | |||
| 128 | 77 | protected static function validatePart(string $part, int $value) |
|
| 134 | |||
| 135 | 54 | public function getMajor() : int |
|
| 139 | |||
| 140 | 50 | public function getMinor() : int |
|
| 144 | |||
| 145 | 46 | public function getPatch() : int |
|
| 149 | |||
| 150 | 29 | public function getPreRelease() : PreRelease |
|
| 154 | |||
| 155 | 7 | public function getBuild() : Build |
|
| 159 | |||
| 160 | 48 | public function isPreRelease() : bool |
|
| 164 | |||
| 165 | 28 | public function isBuild() : bool |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param self|string $version |
||
| 172 | * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal) |
||
| 173 | */ |
||
| 174 | 21 | public function compareTo($version) : int |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param self|string $version |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 5 | public function isEqualTo($version) : bool |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param self|string $version |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 1 | public function isNotEqualTo($version) : bool |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param self|string $version |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 3 | public function isGreaterThan($version) : bool |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param self|string $version |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 4 | public function isGreaterOrEqualTo($version) : bool |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param self|string $version |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 3 | public function isLessThan($version) : bool |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param self|string $version |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 3 | public function isLessOrEqualTo($version) : bool |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param ConstraintInterface|string $constraint |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 3 | public function matches($constraint) : bool |
|
| 249 | |||
| 250 | 1 | public function incrementMajor() : Version |
|
| 254 | |||
| 255 | 2 | public function incrementMinor() : Version |
|
| 259 | |||
| 260 | 1 | public function incrementPatch() : Version |
|
| 264 | |||
| 265 | 2 | public function withPreRelease($preRelease) : Version |
|
| 269 | |||
| 270 | 1 | public function withBuild($build) : Version |
|
| 274 | |||
| 275 | 14 | public function getVersionString() : string |
|
| 285 | |||
| 286 | 10 | public function __toString() : string |
|
| 290 | |||
| 291 | 4 | public function jsonSerialize() : string |
|
| 295 | |||
| 296 | 4 | public function toArray() : array |
|
| 306 | |||
| 307 | 21 | public static function getComparator() : ComparatorInterface |
|
| 315 | |||
| 316 | 1 | public static function setComparator(ComparatorInterface $comparator) : void |
|
| 320 | } |
||
| 321 |