Version::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 5
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the egabor/composer-release-plugin package.
5
 *
6
 * (c) Gábor Egyed <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace egabor\Composer\ReleasePlugin;
13
14
use egabor\Composer\ReleasePlugin\Exception\InvalidVersionException;
15
16
final class Version extends SemVer
17
{
18
    private static $supportedPreLevels = [
19
        'alpha',
20
        'beta',
21
        'rc',
22
    ];
23
    private $preReleaseVersionParts;
24
25 116
    public function __construct($major, $minor, $patch, $pre = '', $build = '')
26
    {
27 116
        parent::__construct($major, $minor, $patch, $pre, $build);
28
29 109
        $this->assertValidVersion();
30 104
        $this->preReleaseVersionParts = '' !== $this->getPre() ? self::parsePreReleaseVersion($this->getPre()) : ['level' => '', 'separator' => '', 'number' => ''];
31 103
    }
32
33 36
    public function getPreLevel()
34
    {
35 36
        return $this->preReleaseVersionParts['level'];
36
    }
37
38 36
    public function getPreSeparator()
39
    {
40 36
        return $this->preReleaseVersionParts['separator'];
41
    }
42
43 34
    public function getPreNumber()
44
    {
45 34
        return $this->preReleaseVersionParts['number'];
46
    }
47
48 25
    public function isStable()
49
    {
50 25
        return 0 < $this->getMajor() && '' === $this->getPre();
51
    }
52
53 37
    private static function parsePreReleaseVersion($tag)
54
    {
55 37
        $pattern = '(?P<level>'.implode('|', self::$supportedPreLevels).')(?P<separator>\\.?)(?P<number>\\d*)';
56 37
        $matches = self::match($pattern, $tag, 'Pre-release version is not valid and cannot be parsed.');
57
58 36
        return array_replace(['level' => '', 'separator' => '', 'number' => ''], $matches);
59
    }
60
61 109
    private function assertValidVersion()
62
    {
63 109
        if ('0' === $this->getMajor() && '' !== $this->getPre()) {
64 1
            throw new InvalidVersionException('Invalid version. Pre-release versions can not have pre part.');
65
        }
66
67 108
        if ('0' !== $this->getPatch() && '' !== $this->getPre()) {
68 4
            throw new InvalidVersionException('Invalid version. Patch versions can not have pre part.');
69
        }
70 104
    }
71
}
72