Passed
Branch master (283c0c)
by Nelson
02:38
created

Version::getRevision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Class definition:  [NelsonMartell]  Version
7
 *
8
 * Copyright © 2015-2017 Nelson Martell (http://nelson6e65.github.io)
9
 *
10
 * Licensed under The MIT License (MIT)
11
 * For full copyright and license information, please see the LICENSE
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright 2015-2017 Nelson Martell
15
 * @link      http://nelson6e65.github.io/php_nml/
16
 * @since     v0.1.1
17
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
18
 * */
19
20
namespace NelsonMartell;
21
22
use \InvalidArgumentException;
23
24
/**
25
 * Representa el número de versión de un programa o ensamblado, de la forma "1.2.3.4". Sólo
26
 * siendo obligatorios el primer y segundo componente.
27
 * No se puede heredar esta clase.
28
 *
29
 * @author Nelson Martell <[email protected]>
30
 * */
31
final class Version extends Object implements IEquatable, IComparable
32
{
33
34
    /**
35
     * Crea una nueva instancia con los números principal, secundario, de
36
     * compilación (opcional) y revisión (opcional).
37
     * Para comprobar si la versión es válida, usar el método isValid.
38
     *
39
     * @param int                              $major    Componente principal
40
     * @param int                              $minor    Componente secundario
41
     * @param int|string|VersionComponent|null $build    Componente de compilación
42
     * @param int|string|VersionComponent|null $revision Componente de revisión
43
     *
44
     * @throws InvalidArgumentException
45
     * */
46 32
    public function __construct($major, $minor, $build = null, $revision = null)
47
    {
48 32
        parent::__construct();
49 32
        unset($this->Major, $this->Minor, $this->Build, $this->Revision);
50
51 32
        if (!is_integer($major)) {
52
            $args = [
53 3
                'class'    => typeof($this)->Name,
54 3
                'name'     => 'major',
55 3
                'pos'      => 0,
56 3
                'expected' => typeof(0),
57 3
                'actual'   => typeof($major),
58
            ];
59
60 3
            $msg = msg('Invalid argument type.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61 3
            $msg .= msg(
62 3
                ' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.',
63 3
                $args
64
            );
65 3
            $msg .= msg(' Convert value or use the "{class}::parse" (static) method.', $args);
66
67 3
            throw new InvalidArgumentException($msg);
68
        }
69
70 29
        if (!is_integer($minor)) {
71
            $args = [
72 2
                'class'    => typeof($this)->Name,
73 2
                'name'     => 'minor',
74 2
                'pos'      => 1,
75 2
                'expected' => typeof(0),
76 2
                'actual'   => typeof($minor),
77
            ];
78
79 2
            $msg = msg('Invalid argument type.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80 2
            $msg .= msg(
81 2
                ' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.',
82 2
                $args
83
            );
84 2
            $msg .= msg(' Convert value or use the "{class}::parse" (static) method.', $args);
85
86 2
            throw new InvalidArgumentException($msg);
87
        }
88
89 27 View Code Duplication
        if ($major < 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $args = [
91 1
                'name'     => 'major',
92 1
                'pos'      => 0,
93 1
                'actual'   => $major,
94
            ];
95
96 1
            $msg = msg('Invalid argument value.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
97 1
            $msg .= msg(
98 1
                ' "{name}" (position {pos}) must to be a positive number; "{actual}" given.',
99 1
                $args
100
            );
101
102 1
            throw new InvalidArgumentException($msg);
103
        }
104
105 26 View Code Duplication
        if ($minor < 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $args = [
107 1
                'name'     => 'minor',
108 1
                'pos'      => 1,
109 1
                'actual'   => $minor,
110
            ];
111
112 1
            $msg = msg('Invalid argument value.');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
113 1
            $msg .= msg(
114 1
                ' "{name}" (position {pos}) must to be a positive number; "{actual}" given.',
115 1
                $args
116
            );
117
118 1
            throw new InvalidArgumentException($msg);
119
        }
120
121 25
        $this->major = $major;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
122 25
        $this->minor = $minor;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
123 25
        $this->build = VersionComponent::Parse($build);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124 24
        $this->revision = VersionComponent::Parse($revision);
125 22
    }
126
127
    /**
128
     * Convierte una cadena a su representación del tipo Version.
129
     *
130
     * @param Version|string|int|float|array $value Objeto a convertir.
131
     *
132
     * @return Version Objeto convertido desde $value.
133
     * */
134 11
    public static function parse($value)
135
    {
136 11
        if ($value instanceof Version) {
137
            return $value;
138
        }
139
140 11
        $version = [];
141
142
        // Try to convert into an array
143 11
        if (is_integer($value)) {
144
            // Integer for major value
145
            $version = [$value, 0];
146 11
        } elseif (is_float($value)) {
147
            // Integer part as major, and decimal part as minor
148
            $version = sprintf("%F", $value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal %F does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
149
            $version = explode('.', $version);
150 11
        } elseif (is_array($value)) {
151
            // Implode first 4 places for major, minor, build and revision respectivally.
152 5
            $version = array_slice($value, 0, 4);
153 6
        } elseif (is_string($value)) {
154 6
            $version = explode('.', $value);
155
        } else {
156
            $msg = msg('Unable to parse. Argument passed has an invalid type: "{0}".', typeof($value));
157
            throw new InvalidArgumentException($msg);
158
        }
159
160
        // $value ya debería ser un array.
161 11
        $c = count($version);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
162
163 11
        if ($c > 4 || $c < 2) {
164 4
            $msg = msg('Unable to parse. Argument passed has an invalid format: "{0}".', $value);
165
            //var_dump($version);
166 4
            throw new InvalidArgumentException($msg);
167
        }
168
169
170 8
        $major = (int) $version[0];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
171 8
        $minor = (int) $version[1];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
172 8
        $build = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
173 8
        $revision = null;
174
175 8
        if (count($version) >= 3) {
176 6
            $build = VersionComponent::Parse($version[2]);
177
178 6
            if (count($version) == 4) {
179 1
                $revision = VersionComponent::Parse($version[3]);
180
            }
181
        }
182
183 8
        return new Version($major, $minor, $build, $revision);
184
    }
185
186
    /**
187
     * Obtiene el valor del componente principal del número de versión del
188
     * objeto actual.
189
     * Esta propiedad es de sólo lectura.
190
     *
191
     * @var int Componente principal del número de versión.
192
     * */
193
    public $Major;
194
    private $major;
195
196
    /**
197
     * Getter for Major property.
198
     *
199
     * @return int
200
     * @see    Version::$major
201
     */
202 56
    public function getMajor()
203
    {
204 56
        return $this->major;
205
    }
206
207
208
    /**
209
     * Obtiene el valor del componente secundario del número de versión del
210
     * objeto actual.
211
     * Esta propiedad es de sólo lectura.
212
     *
213
     * @var int Componente secundario del número de versión.
214
     * */
215
    public $Minor;
216
    private $minor;
217
218
    /**
219
     * Getter for minor property.
220
     *
221
     * @return int
222
     * @see    Version::$minor
223
     */
224 35
    public function getMinor()
225
    {
226 35
        return $this->minor;
227
    }
228
229
    /**
230
     * Obtiene el valor del componente de compilación del número de versión
231
     * del objeto actual.
232
     * Esta propiedad es de sólo lectura.
233
     *
234
     * @var VersionComponent Componente de compilación del número de versión.
235
     * */
236
    public $Build;
237
    private $build;
238
239
    /**
240
     * Getter for Build property.
241
     *
242
     * @return VersionComponent
243
     * @see    Version::$build
244
     */
245 47
    public function getBuild()
246
    {
247 47
        return $this->build;
248
    }
249
250
    /**
251
     * Obtiene el valor del componente de revisión del número de versión del
252
     * objeto actual.
253
     * Esta propiedad es de sólo lectura.
254
     *
255
     * @var VersionComponent Componente de revisión del número de versión.
256
     * */
257
    public $Revision;
258
    private $revision;
259
260
    /**
261
     * Getter for Revision property.
262
     *
263
     * @return VersionComponent
264
     * @see    Version::$revision
265
     */
266 42
    public function getRevision()
267
    {
268 42
        return $this->revision;
269
    }
270
271
272
    /**
273
     * Convierte la instancia actual en su representación en cadena.
274
     * Por defecto, si no están definidos los componentes de compilación y
275
     * revisión, no se incluyen en la salida.
276
     * Use el método isValid si quiere determinar si la versión es válida
277
     * antes de devolver esta cadena.
278
     *
279
     * @return string Representación de la versión en forma de cadena:
280
     *   'major.minor[.build[.revision]]'
281
     * @see    VersionComponent::isNull()
282
     * @see    Version::isValid()
283
     * */
284 16
    public function toString()
285
    {
286 16
        $s[0] = $this->Major;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$s was never initialized. Although not strictly required by PHP, it is generally a good practice to add $s = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $s. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
287 16
        $s[1] = $this->Minor;
288
289 16
        if ($this->Revision->IsNotNull()) {
290 6
            $s[2] = $this->Build;
291 6
            $s[3] = $this->Revision;
292
        } else {
293 10
            if ($this->Build->IsNotNull()) {
294 6
                $s[2] = $this->Build;
295
            }
296
        }
297 16
        $v = implode('.', $s);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $v. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
298
299 16
        return $v;
300
    }
301
302
    /**
303
     * Indica si la instancia actual es un número de versión válido.
304
     *
305
     * Se considera válido si:
306
     * 1. Major o Minor es mayor a cero (0). No puede ser '0.0'.
307
     * 2. Build y Revision son nulos (no están definidos).
308
     * 3. Build está definido pero Revision no.
309
     * 4. Ambos están definidos, pero no poseen la parte de la cadena.
310
     * 5. Ambos están definidos, pero Build no posee la parte de cadena.
311
     * 6. Build está definido y tiene la cadena, pero Revision no está definido.
312
     * 7. Revision posee cadena, pero Build no.
313
     *
314
     * @return boolean Un valor que indica si la instancia actual es válida.
315
     * */
316 16
    public function isValid()
317
    {
318
        // Validación de Major y Minor:
319 16
        $r = ($this->Major > 0 or $this->Minor > 0); //#1
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
320
321
        // Validación de Build y Revision:
322 16
        if ($r) {
323 15
            $r = ($this->Build->IsNull() and $this->Revision->IsNull()); // #2
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
324
325 15
            if (!$r) {
326 13
                if ($this->Build->IsNotNull() and $this->Revision->IsNotNull()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
327
                    // Si ambos están definidos...
328
329 9
                    $r = (bool) ($this->Build->StringValue == ''); //#5
330
331 9
                    if (!$r) {
332
                        //#4
333 6
                        $r = (bool) (($this->Build->StringValue == '') and ($this->Revision->StringValue == ''));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
334
335 6
                        if (!$r) {
336 6
                            if ($this->Build->StringValue != '') {
337 6
                                $r = $this->Revision->IsNull(); #6
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
338
                            }
339
340 6
                            if ($this->Revision->StringValue != '') {
341 3
                                $r = ($this->Build->StringValue == ''); #7
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
342
                            }
343
                        }
344
                    }
345
                } else {
346 4
                    $r = ($this->Build->IsNotNull() and $this->Revision->IsNull()); //#3
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
347
                }
348
            }
349
        }
350
351 16
        return (bool) $r;
352
    }
353
354
    /**
355
     * Determina si el objeto $other especificado es igual a la instancia actual.
356
     *
357
     * @param Version $other El otro objeto a comparar.
358
     *
359
     * @return bool `true` si $other es igual esta instancia; caso contrario,
360
     *   `false`.
361
     * */
362 32
    public function equals($other)
363
    {
364 32
        if ($other instanceof Version) {
365 24
            if ($this->Major == $other->Major && $this->Minor == $other->Minor) {
366 16
                if ($this->Build->equals($other->Build)) {
367 11
                    if ($this->Revision->equals($other->Revision)) {
368 10
                        return true;
369
                    }
370
                }
371
            }
372
        }
373
374 29
        return false;
375
    }
376
377
378
    #region IComparable
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
379
380
    /**
381
     * Determina la posición relativa de esta instancia con respecto al objeto especificado.
382
     *
383
     * For types different than ``Version``:
384
     * - ``integer`` and ``null`` are always < 0;
385
     * - ``string`` and ``array`` are parsed and then evaluated (if is not parseable, always > 0);
386
     * - other types are always > 0
387
     *
388
     * @param Version|int|string|mixed $other
389
     *   The other object to compare with.
390
     *
391
     * @return integer|null
392
     *   Returns:
393
     *   - ``= 0`` if this instance is considered equivalent to $other;
394
     *   - ``> 0`` si esta instancia se considera mayor a $other;
395
     *   - ``< 0`` si esta instancia se considera menor a $other.
396
     *   - ``null`` if this instance can't be compared against $other .
397
     * @see Object::compare()
398
     * */
399 24
    public function compareTo($other)
400
    {
401 24
        $r = $this->equals($other) ? 0 : 9999;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
402
403 24
        if (!($other instanceof Version)) {
404 17
            switch (typeof($other)->toString()) {
405 17
                case 'integer':
406 14
                case 'float':
407 14
                case 'double':
408 13
                case 'null':
409 13
                case 'NULL':
410 4
                    $r = 1; // Siempre es mayor a cualquier número o null
411 4
                    break;
412
413 13
                case 'string':
414 7
                case 'array':
415
                    // Se tratan de convertir las cadenas y arrays
416
                    try {
417 11
                        $tmp = Version::parse($other);
418 8
                        $r = $this->compareTo($tmp);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
419 4
                    } catch (InvalidArgumentException $e) {
420
                        // Siempre es mayor a strings o arrays que no se puedan convertir
421 4
                        $r = 1;
422
                    }
423 11
                    break;
424
425
                default:
426
                    // No se puede determinar comparando a otros objetos.
427 2
                    $r = null;
428
            }
429
430 17
            return $r;
431
        }
432
433 16
        if ($r !== 0) {
434 14
            $r = $this->Major - $other->Major;
435
436 14
            if ($r === 0) {
437 7
                $r = $this->Minor - $other->Minor;
438
439 7
                if ($r === 0) {
440 6
                    $r = $this->Build->compareTo($other->Build);
441
442 6
                    if ($r === 0) {
443 1
                        $r = $this->Revision->compareTo($other->Revision);
444
                    }
445
                }
446
            }
447
        }
448
449 16
        return $r;
450
    }
451
452
    #endregion
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
453
}
454