Semver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 0
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createFromString() 0 16 3
A getCurrent() 0 7 1
A getNextPatchRelease() 0 9 2
A getNextMinorRelease() 0 12 2
A getNextMajorRelease() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Selami\Stdlib;
6
7
use Selami\Stdlib\Exception\InvalidSemverPatternException;
8
9
use function preg_match;
10
use function sprintf;
11
12
/**
13
 * Semantic Versioning 2.0.0 compatible version handling
14
 */
15
final class Semver
16
{
17
    private static $semverPattern = '/(\d+).(\d+).(\d+)(|[-.+](?:dev|alpha|beta|rc|stable)(.*?))$/i';
18
    private $major;
19
    private $minor;
20
    private $patch;
21
    private $preRelease;
22
23
    private function __construct(int $major, int $minor, int $patch, ?string $preRelase = null)
24
    {
25
        $this->major      = $major;
26
        $this->minor      = $minor;
27
        $this->patch      = $patch;
28
        $this->preRelease = $preRelase;
29
    }
30
31
    public static function createFromString(string $version): self
32
    {
33
        $preRelease = null;
34
        if (! preg_match(self::$semverPattern, $version, $match)) {
35
            throw new InvalidSemverPatternException(
36
                sprintf('%s is not valid semver compatible version name', $version)
37
            );
38
        }
39
40
        [, $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...
41
        if ($preRelease === '') {
42
            $preRelease = null;
43
        }
44
45
        return new self((int) $major, (int) $minor, (int) $patch, $preRelease);
46
    }
47
48
    public function getCurrent(): string
49
    {
50
        return $this->major . '.'
51
            . $this->minor . '.'
52
            . $this->patch
53
            . $this->preRelease;
54
    }
55
56
    public function getNextPatchRelease(): string
57
    {
58
        $patch = $this->patch;
59
        if ($this->preRelease === null) {
60
            $patch++;
61
        }
62
63
        return $this->major . '.' . $this->minor . '.' . $patch;
64
    }
65
66
    public function getNextMinorRelease(): string
67
    {
68
        $minor = $this->minor;
69
        $patch = $this->patch;
70
        if ($this->preRelease === null) {
71
            $minor++;
72
73
            $patch = 0;
74
        }
75
76
        return $this->major . '.' . $minor . '.' . $patch;
77
    }
78
79
    public function getNextMajorRelease(): string
80
    {
81
        $major = $this->major;
82
        $minor = $this->minor;
83
        $patch = $this->patch;
84
        if ($this->preRelease === null) {
85
            $major++;
86
            $minor = 0;
87
            $patch = 0;
88
        }
89
90
        return $major . '.' . $minor . '.' . $patch;
91
    }
92
}
93