GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 489def...baee45 )
by Martin
04:39
created

Version::getCompleteParts()   C

Complexity

Conditions 11
Paths 36

Size

Total Lines 45
Code Lines 24

Duplication

Lines 6
Ratio 13.33 %

Code Coverage

Tests 25
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 45
ccs 25
cts 25
cp 1
rs 5.2653
cc 11
eloc 24
nc 36
nop 1
crap 11

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
namespace UserAgentParser\Model;
3
4
class Version
5
{
6
    /**
7
     *
8
     * @var integer
9
     */
10
    private $major;
11
12
    /**
13
     *
14
     * @var integer
15
     */
16
    private $minor;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    private $patch;
23
24
    /**
25
     *
26
     * @var string
27
     */
28
    private $alias;
29
30
    /**
31
     *
32
     * @var string
33
     */
34
    private $complete;
35
36
    private static $notAllowedAlias = [
37
        'a',
38
        'alpha',
39
        'prealpha',
40
41
        'b',
42
        'beta',
43
        'prebeta',
44
45
        'rc',
46
    ];
47
48
    /**
49
     *
50
     * @param integer $major
51
     */
52 8
    public function setMajor($major)
53
    {
54 8
        if ($major !== null) {
55 6
            $major = (int) $major;
56
        }
57
58 8
        $this->major = $major;
59
60 8
        $this->hydrateComplete();
61 8
    }
62
63
    /**
64
     *
65
     * @return integer
66
     */
67 8
    public function getMajor()
68
    {
69 8
        return $this->major;
70
    }
71
72
    /**
73
     *
74
     * @param integer $minor
75
     */
76 8
    public function setMinor($minor)
77
    {
78 8
        if ($minor !== null) {
79 6
            $minor = (int) $minor;
80
        }
81
82 8
        $this->minor = $minor;
83
84 8
        $this->hydrateComplete();
85 8
    }
86
87
    /**
88
     *
89
     * @return integer
90
     */
91 7
    public function getMinor()
92
    {
93 7
        return $this->minor;
94
    }
95
96
    /**
97
     *
98
     * @param integer $patch
99
     */
100 8
    public function setPatch($patch)
101
    {
102 8
        if ($patch !== null) {
103 5
            $patch = (int) $patch;
104
        }
105
106 8
        $this->patch = $patch;
107
108 8
        $this->hydrateComplete();
109 8
    }
110
111
    /**
112
     *
113
     * @return integer
114
     */
115 7
    public function getPatch()
116
    {
117 7
        return $this->patch;
118
    }
119
120
    /**
121
     *
122
     * @param string $alias
123
     */
124 8
    public function setAlias($alias)
125
    {
126 8
        $this->alias = $alias;
127
128 8
        $this->hydrateComplete();
129 8
    }
130
131
    /**
132
     *
133
     * @return string
134
     */
135 8
    public function getAlias()
136
    {
137 8
        return $this->alias;
138
    }
139
140
    /**
141
     * Set from the complete version string.
142
     *
143
     * @param string $complete
144
     */
145 7
    public function setComplete($complete)
146
    {
147
        // check if the version has only 0 -> so no real result
148
        // maybe move this out to the Providers itself?
149 7
        $left = preg_replace('/[0.]/', '', $complete);
150 7
        if ($left === '') {
151 2
            $complete = null;
152
        }
153
154 7
        $this->hydrateFromComplete($complete);
155
156 7
        $this->complete = $complete;
157 7
    }
158
159
    /**
160
     *
161
     * @return string
162
     */
163 7
    public function getComplete()
164
    {
165 7
        return $this->complete;
166
    }
167
168
    /**
169
     *
170
     * @return array
171
     */
172 1
    public function toArray()
173
    {
174
        return [
175 1
            'major' => $this->getMajor(),
176 1
            'minor' => $this->getMinor(),
177 1
            'patch' => $this->getPatch(),
178
179 1
            'alias' => $this->getAlias(),
180
181 1
            'complete' => $this->getComplete(),
182 1
        ];
183
    }
184
185
    /**
186
     *
187
     * @return string
188
     */
189 8
    private function hydrateComplete()
190
    {
191 8
        if ($this->getMajor() === null && $this->getAlias() === null) {
192 3
            return;
193
        }
194
195 7
        $version = $this->getMajor();
196
197 7
        if ($this->getMinor() !== null) {
198 6
            $version .= '.' . $this->getMinor();
199
        }
200
201 7
        if ($this->getPatch() !== null) {
202 5
            $version .= '.' . $this->getPatch();
203
        }
204
205 7
        if ($this->getAlias() !== null) {
206 4
            $version = $this->getAlias() . ' - ' . $version;
207
        }
208
209 7
        $this->complete = $version;
210 7
    }
211
212 7
    private function hydrateFromComplete($complete)
213
    {
214 7
        $parts = $this->getCompleteParts($complete);
215
216 7
        $this->setMajor($parts['major']);
217 7
        $this->setMinor($parts['minor']);
218 7
        $this->setPatch($parts['patch']);
219 7
        $this->setAlias($parts['alias']);
220 7
    }
221
222
    /**
223
     *
224
     * @return array
225
     */
226 7
    private function getCompleteParts($complete)
227
    {
228
        $versionParts = [
229 7
            'major' => null,
230 7
            'minor' => null,
231 7
            'patch' => null,
232
233 7
            'alias' => null,
234 7
        ];
235
236
        // only digits
237 7
        preg_match("/\d+(?:\.*\d*)*/", $complete, $result);
238 7
        if (count($result) > 0) {
239 5
            $parts = explode('.', $result[0]);
240
241 5
            if (isset($parts[0]) && $parts[0] != '') {
242 5
                $versionParts['major'] = (int) $parts[0];
243
            }
244 5 View Code Duplication
            if (isset($parts[1]) && $parts[1] != '') {
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...
245 5
                $versionParts['minor'] = (int) $parts[1];
246
            }
247 5 View Code Duplication
            if (isset($parts[2]) && $parts[2] != '') {
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...
248 4
                $versionParts['patch'] = (int) $parts[2];
249
            }
250
        }
251
252
        // grab alias
253 7
        $result = preg_split("/\d+(?:\.*\d*)*/", $complete);
254 7
        foreach ($result as $row) {
255 7
            $row = trim($row);
256
257 7
            if ($row === '') {
258 6
                continue;
259
            }
260
261
            // do not use beta and other things
262 5
            if (in_array($row, self::$notAllowedAlias)) {
263 2
                continue;
264
            }
265
266 3
            $versionParts['alias'] = $row;
267 7
        }
268
269 7
        return $versionParts;
270
    }
271
}
272