Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.52% covered (warning)
64.52%
20 / 31
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Version
64.52% covered (warning)
64.52%
20 / 31
28.57% covered (danger)
28.57%
2 / 7
20.55
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 newFromString
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
2.00
 getPrettyVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBranch
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getGitBranch
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __toString
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 equals
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Copyright (C) 2016, 2021 Kunal Mehta <legoktm@debian.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18namespace MediaWiki\VersionInfo;
19
20/**
21 * Represents a version in the format
22 * major.minor.patch. Note that patch might
23 * be "x", to represent a branch of releases
24 */
25class Version {
26
27    /**
28     * @var int
29     */
30    public $major;
31
32    /**
33     * @var int
34     */
35    public $minor;
36
37    /**
38     * @var int|string "x" if this represents a branch
39     */
40    public $patch;
41
42    /**
43     * @param int|string $major
44     * @param int|string $minor
45     * @param int|string $patch
46     */
47    public function __construct( $major, $minor, $patch ) {
48        // Cast everything to int out of safety
49        $this->major = (int)$major;
50        $this->minor = (int)$minor;
51        $this->patch = $patch === 'x' ? $patch : (int)$patch;
52    }
53
54    /**
55     * @param string $input in ##.##.## format
56     * @throws \Exception
57     * @return bool|Version
58     */
59    public static function newFromString( $input ) {
60        $matched = preg_match(
61            '/^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))?$/',
62            $input,
63            $matches
64        );
65        if ( !$matched ) {
66            // Invalid
67            throw new \Exception( "Invalid version: $input" );
68        }
69
70        $patch = $matches['patch'] ?? 'x';
71
72        return new self(
73            $matches['major'],
74            $matches['minor'],
75            $patch
76        );
77    }
78
79    /**
80     * String representation for human output
81     *
82     * @return string
83     */
84    public function getPrettyVersion() {
85        return "{$this->major}.{$this->minor}.{$this->patch}";
86    }
87
88    /**
89     * Get this release's branch (e.g. 1.36)
90     *
91     * @return Version
92     */
93    public function getBranch() {
94        if ( $this->patch === 'x' ) {
95            return $this;
96        } else {
97            $fam = clone $this;
98            $fam->patch = 'x';
99            return $fam;
100        }
101    }
102
103    /**
104     * Git Branch for this version (e.g. REL1_37)
105     *
106     * @return string
107     */
108    public function getGitBranch() {
109        return "REL{$this->major}_{$this->minor}";
110    }
111
112    /**
113     * String representation for database output, zero-padded
114     * so sorting is easier
115     *
116     * @return string
117     */
118    public function __toString() {
119        // 0-pad minor and patch to 2 digits
120        $minor = sprintf( '%02d', $this->minor );
121        if ( $this->patch === 'x' ) {
122            $patch = '';
123        } else {
124            $patch = '.' . sprintf( '%02d', $this->patch );
125        }
126        return "{$this->major}.{$minor}{$patch}";
127    }
128
129    /**
130     * Whether it equals the other version exactly or not
131     * @param Version $other
132     * @return bool
133     */
134    public function equals( Version $other ) {
135        return $this->major === $other->major
136            && $this->minor === $other->minor
137            && $this->patch === $other->patch;
138    }
139}