Completed
Push — master ( 53c7ee...8b653d )
by Mehmet
09:05
created

Semver::getNextMajorRelease()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.9666
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Stdlib;
5
6
use Selami\Stdlib\Exception\InvalidSemverPatternException;
7
8
/**
9
 * Class Semver
10
 * Semantic Versioning 2.0.0 compatible version handling
11
 * @package Selami\Stdlib
12
 */
13
final class Semver
14
{
15
    private static $semverPattern = '/(\d+).(\d+).(\d+)(|[-.+](?:dev|alpha|beta|rc|stable)(.*?))$/i';
16
    private $major;
17
    private $minor;
18
    private $patch;
19
    private $preRelease;
20
21
    private function __construct(int $major, int $minor, int $patch, ?string $preRelase = null)
22
    {
23
        $this->major = $major;
24
        $this->minor = $minor;
25
        $this->patch = $patch;
26
        $this->preRelease = $preRelase;
27
    }
28
29
    public static function createFromString(string $version) : self
30
    {
31
        $preRelease = null;
32
        if (!preg_match(self::$semverPattern, $version, $match)) {
33
            throw new InvalidSemverPatternException(
34
                sprintf('%s is not valid semver compatible version name', $version)
35
            );
36
        }
37
        [,$major, $minor, $patch, $preRelease] = $match;
0 ignored issues
show
Bug introduced by
The variable $major does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $minor does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $patch does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
        if ($preRelease === '') {
39
            $preRelease = null;
40
        }
41
        return new self((int) $major, (int) $minor, (int) $patch, $preRelease);
42
    }
43
44
    public function getCurrent() : string
45
    {
46
        return $this->major . '.'
47
            . $this->minor . '.'
48
            . $this->patch
49
            . $this->preRelease;
50
    }
51
52
    public function getNextPatchRelease() : string
53
    {
54
        return $this->major . '.' . $this->minor . '.' . (++$this->patch);
55
    }
56
57
    public function getNextMinorRelease() : string
58
    {
59
        return $this->major . '.' . (++$this->minor) . '.' . $this->patch;
60
    }
61
62
    public function getNextMajorRelease() : string
63
    {
64
        if ($this->preRelease === null) {
65
            $this->major++;
66
        }
67
        return $this->major . '.'
68
            . $this->minor . '.'
69
            . $this->patch;
70
    }
71
}
72