Completed
Pull Request — master (#34)
by Pádraic
03:01
created

VersionParser::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Humbug
4
 *
5
 * @category   Humbug
6
 * @package    Humbug
7
 * @copyright  Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
8
 * @license    https://github.com/padraic/phar-updater/blob/master/LICENSE New BSD License
9
 *
10
 * This class is partially patterned after Composer's version parser.
11
 */
12
13
namespace Humbug\SelfUpdate;
14
15
use Composer\Semver\Semver;
16
use Composer\Semver\VersionParser as Parser;
17
18
class VersionParser
19
{
20
21
    /**
22
     * @var array
23
     */
24
    private $versions;
25
26
    /**
27
     * @var Composer\VersionParser
28
     */
29
    private $parser;
30
31
    /**
32
     * @var string
33
     */
34
    private const GIT_DATA_MATCH = '/.*(-\d+-g[[:alnum:]]{7})$/';
35
36
    /**
37
     * @param array $versions
38
     */
39
    public function __construct(array $versions = array())
40
    {
41
        $this->versions = $versions;
42
        $this->parser = new Parser; 
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Composer\Semver\VersionParser() of type object<Composer\Semver\VersionParser> is incompatible with the declared type object<Humbug\SelfUpdate\Composer\VersionParser> of property $parser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * Get the most recent stable numbered version from versions passed to
47
     * constructor (if any)
48
     *
49
     * @return string
50
     */
51
    public function getMostRecentStable()
52
    {
53
        return $this->selectRecentStable();
54
    }
55
56
    /**
57
     * Get the most recent unstable numbered version from versions passed to
58
     * constructor (if any)
59
     *
60
     * @return string
61
     */
62
    public function getMostRecentUnStable()
63
    {
64
        return $this->selectRecentUnstable();
65
    }
66
67
    /**
68
     * Get the most recent stable or unstable numbered version from versions passed to
69
     * constructor (if any)
70
     *
71
     * @return string
72
     */
73
    public function getMostRecentAll()
74
    {
75
        return $this->selectRecentAll();
76
    }
77
78
    /**
79
     * Checks if given version string represents a stable numbered version
80
     *
81
     * @param string $version
82
     * @return bool
83
     */
84
    public function isStable($version)
85
    {
86
        return $this->stable($version);
87
    }
88
89
    /**
90
     * Checks if given version string represents a 'pre-release' version, i.e.
91
     * it's unstable but not development level.
92
     *
93
     * @param string $version
94
     * @return bool
95
     */
96
    public function isPreRelease($version)
97
    {
98
        return !$this->stable($version) && !$this->development($version);
99
    }
100
101
    /**
102
     * Checks if given version string represents an unstable or dev-level
103
     * numbered version
104
     *
105
     * @param string $version
106
     * @return bool
107
     */
108
    public function isUnstable($version)
109
    {
110
        return !$this->stable($version);
111
    }
112
113
    /**
114
     * Checks if given version string represents a dev-level numbered version
115
     *
116
     * @param string $version
117
     * @return bool
118
     */
119
    public function isDevelopment($version)
120
    {
121
        return $this->development($version);
122
    }
123
124
    /**
125
     * Checks if two version strings are the same normalised version.
126
     * 
127
     * @param  string
128
     * @param  string
129
     * @return bool
130
     */
131
    public static function equals($version1, $version2)
132
    {
133
        $parser = new Parser;
134
        return $parser->normalize(self::stripGitHash($version1))
135
            === $parser->normalize(self::stripGitHash($version2)); 
136
    }
137
138
    private function selectRecentStable()
139
    {
140
        $candidates = array();
141
        foreach ($this->versions as $version) {
142
            if (!$this->stable($version)) {
143
                continue;
144
            }
145
            $candidates[] = $version;
146
        }
147
        if (empty($candidates)) {
148
            return false;
149
        }
150
        return $this->findMostRecent($candidates);
151
    }
152
153
    private function selectRecentUnstable()
154
    {
155
        $candidates = array();
156
        foreach ($this->versions as $version) {
157
            if ($this->stable($version) || $this->development($version)) {
158
                continue;
159
            }
160
            $candidates[] = $version;
161
        }
162
        if (empty($candidates)) {
163
            return false;
164
        }
165
        return $this->findMostRecent($candidates);
166
    }
167
168
    private function selectRecentAll()
169
    {
170
        $candidates = array();
171
        foreach ($this->versions as $version) {
172
            if ($this->development($version)) {
173
                continue;
174
            }
175
            $candidates[] = $version;
176
        }
177
        if (empty($candidates)) {
178
            return false;
179
        }
180
        return $this->findMostRecent($candidates);
181
    }
182
183
    private function findMostRecent(array $candidates)
184
    {
185
        $sorted = Semver::rsort($candidates);
186
        return $sorted[0];
187
    }
188
189
    private function stable($version)
190
    {
191
        if ('stable' === Parser::parseStability(self::stripGitHash($version))) {
192
            return true;
193
        }
194
        return false;
195
    }
196
197
    private function development($version)
198
    {
199
        if ('dev' === Parser::parseStability(self::stripGitHash($version))) {
200
            return true;
201
        }
202
        return false;
203
    }
204
205
    private static function stripGitHash($version)
206
    {
207
        if (preg_match(self::GIT_DATA_MATCH, $version, $matches)) {
208
            $version = str_replace($matches[1], '', $version);
209
        }
210
        return $version;
211
    }
212
}
213