Completed
Push — master ( 446f2e...32b2c3 )
by Nelson
11:26
created

VersionComponent::compareTo()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 10
nop 1
dl 0
loc 34
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Class definition:  [NelsonMartell]  VersionComponent
7
 *
8
 * Copyright © 2015-2016 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-2016 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 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
     * */
30
    class VersionComponent extends IntString implements IEquatable
31
    {
32
        /**
33
         *
34
         *
35
         * @param int|null    $intValue
36
         * @param string|null $stringValue
37
         */
38
        public function __construct($intValue = null, $stringValue = null)
39
        {
40
            // Validates filters for only null or int/string value types.
41
            parent::__construct($intValue, $stringValue);
42
43
            if ($intValue === null) {
44
                $this->intValue = $intValue;
45
46
                // Ignore string value if intValue is null.
47
                $this->stringValue = '';
48
            } else {
49
                // Validation of values
50 View Code Duplication
                if ($this->IntValue < 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...
51
                    $args = [
52
                        'position' => '1st',
53
                        'actual'   => $intValue,
54
                    ];
55
56
                    $msg = nml_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...
Deprecated Code introduced by
The function nml_msg() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\msg()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
57
                    $msg .= nml_msg(
0 ignored issues
show
Deprecated Code introduced by
The function nml_msg() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\msg()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
58
                        ' {position} argument must to be a positive number; "{actual}" given.',
59
                        $args
60
                    );
61
62
                    throw new InvalidArgumentException($msg);
63
                } // Integer is valid
64
65
                if ($stringValue !== null) {
66
                    if ($this->StringValue != '') {
67
                        $pattern = '~^([a-z])$~'; // 1 char
68
69
                        if (strlen($this->StringValue) > 1) {
70
                            $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...
71
                            $middle = '([a-z]|[0-9]|-)*';
72
                            $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...
73
74
                            $pattern = $start.$middle.$end;
75
                        }
76
77
                        $correct = (boolean) preg_match($pattern, $this->StringValue);
78
79
                        if ($correct) {
80
                            //Último chequeo: que no hayan 2 '-' consecutivos.
81
                            $correct = strpos($this->StringValue, '--') == false ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($this->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...
82
                        }
83
84
                        if (!$correct) {
85
                            $args = [
86
                                'position' => '2nd',
87
                                'actual'   => $stringValue,
88
                            ];
89
90
                            $msg = nml_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...
Deprecated Code introduced by
The function nml_msg() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\msg()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
91
                            $msg .= nml_msg(
0 ignored issues
show
Deprecated Code introduced by
The function nml_msg() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\msg()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
92
                                ' {position} parameter has invalid chars; "{actual}" given.',
93
                                $args
94
                            );
95
96
                            throw new InvalidArgumentException($msg);
97
                        }
98
                    }
99
                } // String is valid
100
            }
101
        }
102
103
        public static function parse($obj)
104
        {
105
            if ($obj instanceof VersionComponent) {
106
                return $obj;
107
            } else {
108
                if ($obj === null || (is_string($obj) && trim($obj) === '')) {
109
                    return new VersionComponent();
110
                }
111
            }
112
113
            $objConverted = parent::parse($obj);
114
115
            return new VersionComponent($objConverted->IntValue, $objConverted->StringValue);
116
        }
117
118
        /**
119
         * Determina si este componente tiene los valores predeterminados (0).
120
         *
121
         * @return boolean
122
         * */
123
        public function isDefault()
124
        {
125
            if ($this->IntValue === 0) {
126
                if ($this->StringValue === '') {
127
                    return true;
128
                }
129
            }
130
131
            return false;
132
        }
133
134
135
        /**
136
         * Getter method for VersionComponent::IntValue property.
137
         *
138
         * @return integer|NULL
139
         * */
140
        public function getIntValue()
141
        {
142
            return $this->intValue;
143
        }
144
145
146
        /**
147
         * Determina si este componente NO tiene los valores predeterminados.
148
         *
149
         * @return boolean
150
         * */
151
        public function isNotDefault()
152
        {
153
            return !$this->isDefault();
154
        }
155
156
        /**
157
         * Determina si esta instancia es nula.
158
         *
159
         * @return boolean
160
         * */
161
        public function isNull()
162
        {
163
            if ($this->IntValue === null) {
164
                return true;
165
            }
166
167
            return false;
168
        }
169
170
        /**
171
         * Determina si esta instancia NO es nula.
172
         *
173
         * @return boolean
174
         * */
175
        public function isNotNull()
176
        {
177
            return !$this->isNull();
178
        }
179
180
        public function equals($other)
181
        {
182
            if ($other instanceof VersionComponent) {
183
                if ($this->IntValue === $other->IntValue) {
184
                    if ($this->StringValue === $other->StringValue) {
185
                        return true;
186
                    }
187
                }
188
            } else {
189
                return parent::equals($other);
190
            }
191
192
            return false;
193
        }
194
195
        public function compareTo($other)
196
        {
197
            if ($other === null) {
198
                return 1;
199
            } elseif ($this->equals($other)) {
200
                return 0;
201
            } elseif ($other instanceof VersionComponent) {
202
                // null < int
203
                if ($this->isNull()) {
204
                    $r = -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...
205
                } elseif ($other->isNull()) {
206
                    $r = 1;
207
                } else {
208
                    // Here are evaluated as integers
209
                    $r = $this->IntValue - $other->IntValue;
210
211
                    if ($r === 0) {
212
                        $r = strnatcmp($this->StringValue, $other->StringValue);
213
                    }
214
                }
215
            } elseif (is_integer($other) || is_array($other)) {
216
                $r = 1;
217
            } elseif (is_string($other)) {
218
                try {
219
                    $r = $this->compareTo(static::parse($other));
220
                } catch (InvalidArgumentException $e) {
221
                    $r = 1;
222
                }
223
            } else {
224
                $r = null;
225
            }
226
227
            return $r;
228
        }
229
    }
230
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
231