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