VersionManipulator::bumpPatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
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 1
dl 0
loc 7
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\UnstableVersionException;
15
16
/**
17
 * Opinionated version manipulations.
18
 *
19
 * @author Gábor Egyed <[email protected]>
20
 */
21
class VersionManipulator
22
{
23
    private static $supportedLevels = [
24
        'major',
25
        'minor',
26
        'patch',
27
        'stable',
28
        'alpha',
29
        'beta',
30
        'rc',
31
    ];
32
33 53
    public static function bump($level, Version $version)
34
    {
35 53
        switch ($level) {
36 53
            case 'major':
37 10
                return self::bumpMajor($version);
38 43
            case 'minor':
39 10
                return self::bumpMinor($version);
40 33
            case 'patch':
41 10
                return self::bumpPatch($version);
42 23
            case 'stable':
43 10
                return self::toStable($version);
44 13
            case 'alpha':
45 3
            case 'beta':
46 1
            case 'rc':
47 12
                return self::bumpPre($version, $level);
48
            default:
49 1
                throw new \InvalidArgumentException(sprintf('Bumping "%s" version is not supported.', $level));
50
        }
51
    }
52
53 7
    public static function supportsLevel($level)
54
    {
55 7
        return in_array((string) $level, self::$supportedLevels, true);
56
    }
57
58 10
    private static function bumpMajor(Version $current)
59
    {
60 10
        if ('' !== $current->getPre() && '0' !== $current->getMinor()) {
61 1
            throw new UnstableVersionException('Need to release a stable version first.');
62
        }
63
64 9
        if ('' !== $current->getPre()) {
65 1
            return self::toStable($current);
66
        }
67
68 8
        return new Version((int) $current->getMajor() + 1, 0, 0);
69
    }
70
71 10
    private static function bumpMinor(Version $current)
72
    {
73 10
        if ('' !== $current->getPre() && '0' === $current->getMinor()) {
74 1
            throw new UnstableVersionException('Need to release a stable version first.');
75
        }
76
77 9
        if ('' !== $current->getPre()) {
78 1
            return self::toStable($current);
79
        }
80
81 8
        return new Version($current->getMajor(), (int) $current->getMinor() + 1, 0);
82
    }
83
84 10
    private static function bumpPatch(Version $current)
85
    {
86 10
        if ('' !== $current->getPre()) {
87 2
            throw new UnstableVersionException('Need to release a stable version first.');
88
        }
89
90 8
        return new Version($current->getMajor(), $current->getMinor(), (int) $current->getPatch() + 1);
91
    }
92
93 12
    private static function bumpPre(Version $current, $level)
94
    {
95 12
        if ('' === $current->getPre() && '0' === $current->getMajor()) {
96 4
            return new Version(1, 0, 0, $level.'1');
97
        }
98
99 8
        if ('' === $current->getPre()) {
100 4
            return new Version($current->getMajor(), (int) $current->getMinor() + 1, 0, $level.'1');
101
        }
102
103 4
        if ($level === $current->getPreLevel()) {
104 2
            $pre = $level.$current->getPreSeparator().((int) $current->getPreNumber() + 1);
105
        } else {
106 2
            $pre = $level.$current->getPreSeparator().'1';
107
        }
108
109 4
        return new Version($current->getMajor(), $current->getMinor(), $current->getPatch(), $pre);
110
    }
111
112 12
    private static function toStable(Version $current)
113
    {
114 12
        if ($current->isStable()) {
115 4
            throw new \LogicException(sprintf('"%s" version is already stable.', $current));
116
        }
117
118 8
        if ('' !== $current->getPre()) {
119 4
            return new Version($current->getMajor(), $current->getMinor(), $current->getPatch());
120
        }
121
122 4
        return new Version((int) $current->getMajor() + 1, 0, 0);
123
    }
124
}
125