| Total Complexity | 56 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Coverage | 95.92% |
| Changes | 0 | ||
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.
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 |
||
| 41 | final class Version extends StrictObject implements IEquatable, IComparable |
||
| 42 | { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Crea una nueva instancia con los números principal, secundario, de |
||
| 46 | * compilación (opcional) y revisión (opcional). |
||
| 47 | * Para comprobar si la versión es válida, usar el método isValid. |
||
| 48 | * |
||
| 49 | * @param int $major Componente principal |
||
| 50 | * @param int $minor Componente secundario |
||
| 51 | * @param int|string|VersionComponent|null $build Componente de compilación |
||
| 52 | * @param int|string|VersionComponent|null $revision Componente de revisión |
||
| 53 | * |
||
| 54 | * @throws InvalidArgumentException |
||
| 55 | * */ |
||
| 56 | 33 | public function __construct($major, $minor, $build = null, $revision = null) |
|
| 57 | { |
||
| 58 | 33 | parent::__construct(); |
|
| 59 | |||
| 60 | 33 | if (!is_integer($major)) { |
|
| 61 | $args = [ |
||
| 62 | 3 | 'class' => typeof($this)->Name, |
|
| 63 | 3 | 'name' => 'major', |
|
| 64 | 3 | 'pos' => 0, |
|
| 65 | 3 | 'expected' => typeof(0), |
|
| 66 | 3 | 'actual' => typeof($major), |
|
| 67 | ]; |
||
| 68 | |||
| 69 | 3 | $msg = msg('Invalid argument type.'); |
|
| 70 | 3 | $msg .= msg( |
|
| 71 | 3 | ' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.', |
|
|
1 ignored issue
–
show
|
|||
| 72 | 3 | $args |
|
| 73 | ); |
||
| 74 | 3 | $msg .= msg(' Convert value or use the "{class}::parse" (static) method.', $args); |
|
| 75 | |||
| 76 | 3 | throw new InvalidArgumentException($msg); |
|
| 77 | } |
||
| 78 | |||
| 79 | 30 | if (!is_integer($minor)) { |
|
| 80 | $args = [ |
||
| 81 | 2 | 'class' => typeof($this)->Name, |
|
| 82 | 2 | 'name' => 'minor', |
|
| 83 | 2 | 'pos' => 1, |
|
| 84 | 2 | 'expected' => typeof(0), |
|
| 85 | 2 | 'actual' => typeof($minor), |
|
| 86 | ]; |
||
| 87 | |||
| 88 | 2 | $msg = msg('Invalid argument type.'); |
|
| 89 | 2 | $msg .= msg( |
|
| 90 | 2 | ' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.', |
|
|
1 ignored issue
–
show
|
|||
| 91 | 2 | $args |
|
| 92 | ); |
||
| 93 | 2 | $msg .= msg(' Convert value or use the "{class}::parse" (static) method.', $args); |
|
| 94 | |||
| 95 | 2 | throw new InvalidArgumentException($msg); |
|
| 96 | } |
||
| 97 | |||
| 98 | 28 | if ($major < 0) { |
|
| 99 | $args = [ |
||
| 100 | 1 | 'name' => 'major', |
|
| 101 | 1 | 'pos' => 0, |
|
| 102 | 1 | 'actual' => $major, |
|
| 103 | ]; |
||
| 104 | |||
| 105 | 1 | $msg = msg('Invalid argument value.'); |
|
| 106 | 1 | $msg .= msg( |
|
| 107 | 1 | ' "{name}" (position {pos}) must to be a positive number; "{actual}" given.', |
|
| 108 | 1 | $args |
|
| 109 | ); |
||
| 110 | |||
| 111 | 1 | throw new InvalidArgumentException($msg); |
|
| 112 | } |
||
| 113 | |||
| 114 | 27 | if ($minor < 0) { |
|
| 115 | $args = [ |
||
| 116 | 1 | 'name' => 'minor', |
|
| 117 | 1 | 'pos' => 1, |
|
| 118 | 1 | 'actual' => $minor, |
|
| 119 | ]; |
||
| 120 | |||
| 121 | 1 | $msg = msg('Invalid argument value.'); |
|
| 122 | 1 | $msg .= msg( |
|
| 123 | 1 | ' "{name}" (position {pos}) must to be a positive number; "{actual}" given.', |
|
| 124 | 1 | $args |
|
| 125 | ); |
||
| 126 | |||
| 127 | 1 | throw new InvalidArgumentException($msg); |
|
| 128 | } |
||
| 129 | |||
| 130 | 26 | $this->major = $major; |
|
| 131 | 26 | $this->minor = $minor; |
|
| 132 | 26 | $this->build = VersionComponent::parse($build); |
|
| 133 | 25 | $this->revision = VersionComponent::parse($revision); |
|
| 134 | 23 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Convierte una cadena a su representación del tipo Version. |
||
| 138 | * |
||
| 139 | * @param Version|string|int|float|array $value Objeto a convertir. |
||
| 140 | * |
||
| 141 | * @return Version Objeto convertido desde $value. |
||
| 142 | * */ |
||
| 143 | 11 | public static function parse($value) |
|
| 144 | { |
||
| 145 | 11 | if ($value instanceof Version) { |
|
| 146 | return $value; |
||
| 147 | } |
||
| 148 | |||
| 149 | 11 | $version = []; |
|
| 150 | |||
| 151 | // Try to convert into an array |
||
| 152 | 11 | if (is_integer($value)) { |
|
| 153 | // Integer for major value |
||
| 154 | $version = [$value, 0]; |
||
| 155 | 11 | } elseif (is_float($value)) { |
|
| 156 | // Integer part as major, and decimal part as minor |
||
| 157 | $version = sprintf("%F", $value); |
||
| 158 | $version = explode('.', $version); |
||
| 159 | 11 | } elseif (is_array($value)) { |
|
| 160 | // Implode first 4 places for major, minor, build and revision respectivally. |
||
| 161 | 5 | $version = array_slice($value, 0, 4); |
|
| 162 | 6 | } elseif (is_string($value)) { |
|
| 163 | 6 | $version = explode('.', $value); |
|
| 164 | } else { |
||
| 165 | $msg = msg('Unable to parse. Argument passed has an invalid type: "{0}".', typeof($value)); |
||
|
1 ignored issue
–
show
|
|||
| 166 | throw new InvalidArgumentException($msg); |
||
| 167 | } |
||
| 168 | |||
| 169 | // $value ya debería ser un array. |
||
| 170 | 11 | $c = count($version); |
|
| 171 | |||
| 172 | 11 | if ($c > 4 || $c < 2) { |
|
| 173 | 4 | $msg = msg('Unable to parse. Argument passed has an invalid format: "{0}".', $value); |
|
| 174 | //var_dump($version); |
||
| 175 | 4 | throw new InvalidArgumentException($msg); |
|
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | 9 | $major = (int) $version[0]; |
|
| 180 | 9 | $minor = (int) $version[1]; |
|
| 181 | 9 | $build = null; |
|
| 182 | 9 | $revision = null; |
|
| 183 | |||
| 184 | 9 | if (count($version) >= 3) { |
|
| 185 | 7 | $build = VersionComponent::Parse($version[2]); |
|
| 186 | |||
| 187 | 7 | if (count($version) == 4) { |
|
| 188 | 1 | $revision = VersionComponent::Parse($version[3]); |
|
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | 9 | return new Version($major, $minor, $build, $revision); |
|
| 193 | } |
||
| 194 | |||
| 195 | private $major; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Getter for major property. |
||
| 199 | * |
||
| 200 | * @return int |
||
| 201 | * @see Version::$major |
||
| 202 | */ |
||
| 203 | 1 | protected function getMajor() |
|
| 206 | } |
||
| 207 | |||
| 208 | private $minor; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Getter for minor property. |
||
| 212 | * |
||
| 213 | * @return int |
||
| 214 | * @see Version::$minor |
||
| 215 | */ |
||
| 216 | 1 | protected function getMinor() |
|
| 217 | { |
||
| 218 | 1 | return $this->minor; |
|
| 219 | } |
||
| 220 | |||
| 221 | private $build; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Getter for build property. |
||
| 225 | * |
||
| 226 | * @return VersionComponent |
||
| 227 | * @see Version::$build |
||
| 228 | */ |
||
| 229 | 1 | protected function getBuild() |
|
| 230 | { |
||
| 231 | 1 | return $this->build; |
|
| 232 | } |
||
| 233 | |||
| 234 | private $revision; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Getter for revision property. |
||
| 238 | * |
||
| 239 | * @return VersionComponent |
||
| 240 | * @see Version::$revision |
||
| 241 | */ |
||
| 242 | 1 | protected function getRevision() |
|
| 243 | { |
||
| 244 | 1 | return $this->revision; |
|
| 245 | } |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * Convierte la instancia actual en su representación en cadena. |
||
| 250 | * Por defecto, si no están definidos los componentes de compilación y |
||
| 251 | * revisión, no se incluyen en la salida. |
||
| 252 | * Use el método isValid si quiere determinar si la versión es válida |
||
| 253 | * antes de devolver esta cadena. |
||
| 254 | * |
||
| 255 | * @return string Representación de la versión en forma de cadena: |
||
| 256 | * 'major.minor[.build[.revision]]' |
||
| 257 | * @see VersionComponent::isNull() |
||
| 258 | * @see Version::isValid() |
||
| 259 | * */ |
||
| 260 | 16 | public function toString() |
|
| 261 | { |
||
| 262 | 16 | $s[0] = $this->major; |
|
| 263 | 16 | $s[1] = $this->minor; |
|
| 264 | |||
| 265 | 16 | if ($this->revision->isNotNull()) { |
|
| 266 | 6 | $s[2] = $this->build; |
|
| 267 | 6 | $s[3] = $this->revision; |
|
| 268 | } else { |
||
| 269 | 10 | if ($this->build->isNotNull()) { |
|
| 270 | 6 | $s[2] = $this->build; |
|
| 271 | } |
||
| 272 | } |
||
| 273 | 16 | $v = implode('.', $s); |
|
| 274 | |||
| 275 | 16 | return $v; |
|
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Indica si la instancia actual es un número de versión válido. |
||
| 280 | * |
||
| 281 | * Se considera válido si: |
||
| 282 | * 1. major o minor es mayor a cero (0). No puede ser '0.0'. |
||
| 283 | * 2. build y revision son nulos (no están definidos). |
||
| 284 | * 3. build está definido pero revision no. |
||
| 285 | * 4. Ambos están definidos, pero no poseen la parte de la cadena. |
||
| 286 | * 5. Ambos están definidos, pero build no posee la parte de cadena. |
||
| 287 | * 6. build está definido y tiene la cadena, pero revision no está definido. |
||
| 288 | * 7. revision posee cadena, pero build no. |
||
| 289 | * |
||
| 290 | * @return bool Un valor que indica si la instancia actual es válida. |
||
| 291 | * */ |
||
| 292 | 16 | public function isValid() |
|
| 293 | { |
||
| 294 | // Validación de major y minor: |
||
| 295 | 16 | $r = ($this->major > 0 or $this->minor > 0); //#1 |
|
| 296 | |||
| 297 | // Validación de build y revision: |
||
| 298 | 16 | if ($r) { |
|
| 299 | 15 | $r = ($this->build->isNull() and $this->revision->isNull()); // #2 |
|
| 300 | |||
| 301 | 15 | if (!$r) { |
|
| 302 | 13 | if ($this->build->isNotNull() and $this->revision->isNotNull()) { |
|
| 303 | // Si ambos están definidos... |
||
| 304 | |||
| 305 | 9 | $r = (bool) ($this->build->StringValue == ''); //#5 |
|
| 306 | |||
| 307 | 9 | if (!$r) { |
|
| 308 | //#4 |
||
| 309 | 6 | $r = (bool) (($this->build->StringValue == '') and ($this->revision->StringValue == '')); |
|
|
1 ignored issue
–
show
|
|||
| 310 | |||
| 311 | 6 | if (!$r) { |
|
| 312 | 6 | if ($this->build->StringValue != '') { |
|
| 313 | 6 | $r = $this->revision->isNull(); #6 |
|
| 314 | } |
||
| 315 | |||
| 316 | 6 | if ($this->revision->StringValue != '') { |
|
| 317 | 9 | $r = ($this->build->StringValue == ''); #7 |
|
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | } else { |
||
| 322 | 4 | $r = ($this->build->isNotNull() and $this->revision->isNull()); //#3 |
|
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | 16 | return (bool) $r; |
|
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Determina si el objeto $other especificado es igual a la instancia actual. |
||
| 332 | * |
||
| 333 | * @param Version $other El otro objeto a comparar. |
||
| 334 | * |
||
| 335 | * @return bool `true` si $other es igual esta instancia; caso contrario, |
||
| 336 | * `false`. |
||
| 337 | * */ |
||
| 338 | 32 | public function equals($other) |
|
| 339 | { |
||
| 340 | 32 | if ($other instanceof Version) { |
|
| 341 | 25 | if ($this->major == $other->major && $this->minor == $other->minor) { |
|
| 342 | 16 | if ($this->build->equals($other->build)) { |
|
| 343 | 11 | if ($this->revision->equals($other->revision)) { |
|
| 344 | 10 | return true; |
|
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | 29 | return false; |
|
| 351 | } |
||
| 352 | |||
| 353 | |||
| 354 | #region IComparable |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Determina la posición relativa de esta instancia con respecto al objeto especificado. |
||
| 358 | * |
||
| 359 | * For types different than ``Version``: |
||
| 360 | * - ``integer`` and ``null`` are always < 0; |
||
| 361 | * - ``string`` and ``array`` are parsed and then evaluated (if is not parseable, always > 0); |
||
| 362 | * - other types are always > 0 |
||
| 363 | * |
||
| 364 | * @param Version|int|string|mixed $other |
||
| 365 | * The other object to compare with. |
||
| 366 | * |
||
| 367 | * @return int|null |
||
| 368 | * Returns: |
||
| 369 | * - ``= 0`` if this instance is considered equivalent to $other; |
||
| 370 | * - ``> 0`` si esta instancia se considera mayor a $other; |
||
| 371 | * - ``< 0`` si esta instancia se considera menor a $other. |
||
| 372 | * - ``null`` if this instance can't be compared against $other . |
||
| 373 | * @see StrictObject::compare() |
||
| 374 | * */ |
||
| 375 | 24 | public function compareTo($other) |
|
| 426 | } |
||
| 427 | |||
| 428 | #endregion |
||
| 429 | } |
||
| 430 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.