Passed
Push — master ( 209660...779a56 )
by Nelson
02:44
created

VersionComponent::getIntValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Class definition:  [NelsonMartell]  VersionComponent
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     0.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 un componente de un número de Version.
26
 * Extiende la clase IntString, pero restringe los valores que puede tomar.
27
 *
28
 * @author Nelson Martell <[email protected]>
29
 * @since 0.1.1
30
 * */
31
class VersionComponent extends IntString implements IEquatable
32
{
33
    /**
34
     *
35
     *
36
     * @param int|null    $intValue
37
     * @param string|null $stringValue
38
     */
39 50
    public function __construct($intValue = null, $stringValue = null)
40
    {
41
        // Validates filters for only null or int/string value types.
42 50
        parent::__construct($intValue, $stringValue);
43
44 48
        $intValue    = $this->intValue;
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
45 48
        $stringValue = $this->stringValue;
46
47 48
        if ($intValue === null) {
0 ignored issues
show
introduced by
The condition $intValue === null is always false.
Loading history...
48
            // Ignore string value if intValue is null.
49 23
            $stringValue = '';
50
        } else {
51
            // Validation of values
52 39
            if ($intValue < 0) {
53
                $args = [
54 5
                    'position' => '1st',
55 5
                    'actual'   => $intValue,
56
                ];
57
58 5
                $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...
59 5
                $msg .= msg(
60 5
                    ' {position} argument must to be a positive number; "{actual}" given.',
61 5
                    $args
62
                );
63
64 5
                throw new InvalidArgumentException($msg);
65
            } // Integer is valid
66
67 35
            if ($stringValue !== null) {
0 ignored issues
show
introduced by
The condition $stringValue !== null is always true.
Loading history...
68 35
                if ($stringValue != '') {
69 15
                    $pattern = '~^([a-z])$~'; // 1 char
70
71 15
                    if (strlen($stringValue) > 1) {
72 15
                        $start = '~^([a-z]|-)';
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...
73 15
                        $middle = '([a-z]|[0-9]|-)*';
74 15
                        $end = '([a-z]|[0-9])$~';
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...
75
76 15
                        $pattern = $start.$middle.$end;
77
                    }
78
79 15
                    $correct = (boolean) preg_match($pattern, $stringValue);
80
81 15
                    if ($correct) {
82
                        //Último chequeo: que no hayan 2 '-' consecutivos.
83 12
                        $correct = strpos($stringValue, '--') == false ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($stringValue, '--') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
84
                    }
85
86 15
                    if (!$correct) {
87
                        $args = [
88 4
                            'position' => '2nd',
89 4
                            'actual'   => $stringValue,
90
                        ];
91
92 4
                        $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...
93 4
                        $msg .= msg(
94 4
                            ' {position} parameter has invalid chars; "{actual}" given.',
95 4
                            $args
96
                        );
97
98 4
                        throw new InvalidArgumentException($msg);
99
                    }
100
                }
101
            } // String is valid
102
        }
103
104 41
        parent::__construct($intValue, $stringValue);
105 41
    }
106
107 44
    public static function parse($obj)
108
    {
109 44
        if ($obj instanceof VersionComponent) {
110 8
            return $obj;
111
        } else {
112 43
            if ($obj === null || (is_string($obj) && trim($obj) === '')) {
113 20
                return new VersionComponent();
114
            }
115
        }
116
117 37
        $objConverted = parent::parse($obj);
118
119 30
        return new VersionComponent($objConverted->intValue, $objConverted->stringValue);
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
120
    }
121
122
    /**
123
     * Determina si este componente tiene los valores predeterminados (0).
124
     *
125
     * @return bool
126
     * */
127 9
    public function isDefault()
128
    {
129 9
        if ($this->intValue === 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
130 1
            if ($this->stringValue === '') {
131 1
                return true;
132
            }
133
        }
134
135 8
        return false;
136
    }
137
138
    /**
139
     * Determina si este componente NO tiene los valores predeterminados.
140
     *
141
     * @return bool
142
     * */
143 9
    public function isNotDefault()
144
    {
145 9
        return !$this->isDefault();
146
    }
147
148
    /**
149
     * Determina si esta instancia es nula.
150
     *
151
     * @return bool
152
     * */
153 51
    public function isNull()
154
    {
155 51
        if ($this->intValue === null) {
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
156 23
            return true;
157
        }
158
159 39
        return false;
160
    }
161
162
    /**
163
     * Determina si esta instancia NO es nula.
164
     *
165
     * @return bool
166
     * */
167 38
    public function isNotNull()
168
    {
169 38
        return !$this->isNull();
170
    }
171
172 40
    public function equals($other)
173
    {
174 40
        if ($other instanceof VersionComponent) {
175 33
            if ($this->intValue === $other->intValue) {
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
176 28
                if ($this->stringValue === $other->stringValue) {
177 33
                    return true;
178
                }
179
            }
180
        } else {
181 15
            return parent::equals($other);
182
        }
183
184 14
        return false;
185
    }
186
187 21
    public function compareTo($other)
188
    {
189 21
        if ($other === null) {
190 1
            return 1;
191 20
        } elseif ($this->equals($other)) {
192 3
            return 0;
193 18
        } elseif ($other instanceof VersionComponent) {
194
            // null < int
195 11
            if ($this->isNull()) {
196 1
                $r = -1;
197 10
            } elseif ($other->isNull()) {
198 1
                $r = 1;
199
            } else {
200
                // Here are evaluated as integers
201 9
                $r = $this->intValue - $other->intValue;
0 ignored issues
show
Bug Best Practice introduced by
The property $intValue is declared private in NelsonMartell\IntString. Since you implement __get, consider adding a @property or @property-read.
Loading history...
202
203 9
                if ($r === 0) {
204 11
                    $r = strnatcmp($this->stringValue, $other->stringValue);
205
                }
206
            }
207 8
        } elseif (is_integer($other) || is_array($other)) {
208 3
            $r = 1;
209 5
        } elseif (is_string($other)) {
210
            try {
211 3
                $r = $this->compareTo(static::parse($other));
212 2
            } catch (InvalidArgumentException $e) {
213 3
                $r = 1;
214
            }
215
        } else {
216 2
            $r = null;
217
        }
218
219 18
        return $r;
220
    }
221
}
222