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.

Version::setMinor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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