VersionParser::parse()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 8.6845
cc 4
eloc 14
nc 5
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of questocat/version-comparator package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Questocat\VersionComparator;
13
14
class VersionParser
15
{
16
    use ValidatesSemVer;
17
18
    /**
19
     * Parse the semantic version strings.
20
     *
21
     * @param string $versionStr
22
     *
23
     * @throws InvalidVersionException
24
     *
25
     * @return array
26
     */
27 10
    public static function parse($versionStr)
28
    {
29 10
        if (!static::validateVersion($versionStr)) {
30 1
            throw new InvalidVersionException("Invalid version string: {$versionStr}");
31
        }
32
33 9
        $buildMetadata = [];
34 9
        $preRelease = [];
35
36 9
        if (false !== strpos($versionStr, '+')) {
37 4
            list($versionStr, $buildMetadata) = explode('+', $versionStr);
38 4
            $buildMetadata = explode('.', $buildMetadata);
39 4
        }
40
41 9
        if (false !== ($pos = strpos($versionStr, '-'))) {
42 8
            $original = $versionStr;
43 8
            $versionStr = substr($versionStr, 0, $pos);
44 8
            $preRelease = explode('.', substr($original, $pos + 1));
45 8
        }
46
47 9
        list($major, $minor, $patch) = array_map('intval', explode('.', $versionStr));
48
49 9
        return compact('major', 'minor', 'patch', 'preRelease', 'buildMetadata');
50
    }
51
}
52