VersionParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 38
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 24 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