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     * @return bool|Version
57     */
58    public static function newFromString( $input ) {
59        $matched = preg_match(
60            '/^(?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))?$/',
61            $input,
62            $matches
63        );
64        if ( !$matched ) {
65            // Invalid
66            throw new \RuntimeException( "Invalid version: $input" );
67        }
68
69        $patch = $matches['patch'] ?? 'x';
70
71        return new self(
72            $matches['major'],
73            $matches['minor'],
74            $patch
75        );
76    }
77
78    /**
79     * String representation for human output
80     *
81     * @return string
82     */
83    public function getPrettyVersion() {
84        return "{$this->major}.{$this->minor}.{$this->patch}";
85    }
86
87    /**
88     * Get this release's branch (e.g. 1.36)
89     *
90     * @return Version
91     */
92    public function getBranch() {
93        if ( $this->patch === 'x' ) {
94            return $this;
95        } else {
96            $fam = clone $this;
97            $fam->patch = 'x';
98            return $fam;
99        }
100    }
101
102    /**
103     * Git Branch for this version (e.g. REL1_37)
104     *
105     * @return string
106     */
107    public function getGitBranch() {
108        return "REL{$this->major}_{$this->minor}";
109    }
110
111    /**
112     * String representation for database output, zero-padded
113     * so sorting is easier
114     *
115     * @return string
116     */
117    public function __toString() {
118        // 0-pad minor and patch to 2 digits
119        $minor = sprintf( '%02d', $this->minor );
120        if ( $this->patch === 'x' ) {
121            $patch = '';
122        } else {
123            $patch = '.' . sprintf( '%02d', $this->patch );
124        }
125        return "{$this->major}.{$minor}{$patch}";
126    }
127
128    /**
129     * Whether it equals the other version exactly or not
130     * @param Version $other
131     * @return bool
132     */
133    public function equals( Version $other ) {
134        return $this->major === $other->major
135            && $this->minor === $other->minor
136            && $this->patch === $other->patch;
137    }
138}