Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.

Commit 27b0353

Browse files
Version expression improvements:
- Fixed expressions like <=?[0-9]+.(x|[0-9]+)(.x)? - Immediately replace all wildcards when a version is entered
1 parent 974007b commit 27b0353

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/vierbergenlars/SemVer/expression.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class expression {
1010
static protected $range_mask = '%1$s\\s+-\\s+%1$s';
1111
static protected $regexp_mask = '/%s/';
1212
static protected $dirty_regexp_mask = '/^[v= ]*%s$/';
13-
static protected $wildcards = array('x', 'X', '*');
1413
private $chunks = array();
1514

1615
/**
@@ -20,16 +19,18 @@ class expression {
2019
function __construct($versions) {
2120
$versions = preg_replace(sprintf(self::$dirty_regexp_mask, self::$global_single_comparator . '(\\s+-\\s+)?' . self::$global_single_xrange), '$1$2$3', $versions); //Paste comparator and version together
2221
$versions = preg_replace('/\\s+/', ' ', $versions); //Condense multiple spaces to one
22+
$versions = str_replace(array('*', 'X'), 'x', $versions); // All the same wildcards, plz
2323
if (strstr($versions, ' - '))
2424
$versions = self::rangesToComparators($versions); //Replace all ranges with comparators
2525
if (strstr($versions, '~'))
2626
$versions = self::spermiesToComparators($versions); //Replace all spermies with comparators
27-
if (strstr($versions, 'x') || strstr($versions, 'X') || strstr($versions, '*'))
27+
if(strstr($versions, 'x') && (strstr($versions, '<')|| strstr($versions, '>')))
28+
$versions = self::compAndxRangesToComparators($versions);
29+
if (strstr($versions, 'x'))
2830
$versions = self::xRangesToComparators($versions); //Replace all x-ranges with comparators
2931
$or = explode('||', $versions);
3032
foreach ($or as &$orchunk) {
31-
$orchunk = trim($orchunk); //Remove spaces
32-
$and = explode(' ', $orchunk);
33+
$and = explode(' ', trim($orchunk));
3334
foreach ($and as $order => &$achunk) {
3435
$achunk = self::standarizeSingleComparator($achunk);
3536
if (strstr($achunk, ' ')) {
@@ -284,7 +285,42 @@ static private function spermiesToComparatorsCallback($matches) {
284285
}
285286

286287
/**
287-
* Converts matches to named version parts, replaces all wildcards by lowercase x
288+
* Standarizes a bunch of x-ranges with comparators in front of them to comparators
289+
*
290+
* @param string $versions
291+
* @return string
292+
*/
293+
static private function compAndxRangesToComparators($versions) {
294+
$regex = sprintf(self::$regexp_mask, self::$global_single_comparator.self::$global_single_xrange);
295+
return preg_replace_callback($regex, array('self', 'compAndxRangesToComparatorsCallback'), $versions);
296+
}
297+
298+
/**
299+
* Callback for compAndxRangesToComparators()
300+
*
301+
* @internal
302+
* @param array $matches
303+
* @return string
304+
*/
305+
static private function compAndxRangesToComparatorsCallback($matches) {
306+
$comparators = $matches[1];
307+
self::matchesToVersionParts($matches, $major, $minor, $patch, $build, $prtag, 'x', 3);
308+
if($comparators[0] === '<') {
309+
if($major === 'x')
310+
return $comparators.'0';
311+
if($minor === 'x')
312+
return $comparators.$major.'.0';
313+
if($patch === 'x')
314+
return $comparators.$major.'.'.$minor.'.0';
315+
return $comparators.self::constructVersionFromParts(false, $major, $minor, $patch, $build, $prtag);
316+
}
317+
elseif($comparators[0] === '>') {
318+
return $comparators.self::constructVersionFromParts(false, ($major==='x'?0:$major), ($minor==='x'?0:$minor), ($patch==='x'?0:$patch), $build, $prtag);
319+
}
320+
}
321+
322+
/**
323+
* Converts matches to named version parts
288324
* @param array $matches Matches array from preg_match
289325
* @param int|string $major Reference to major version
290326
* @param int|string $minor Reference to minor version
@@ -319,12 +355,6 @@ static protected function matchesToVersionParts($matches, &$major, &$minor, &$pa
319355
$minor = intval($minor);
320356
if (is_numeric($major))
321357
$major = intval($major);
322-
if (in_array($major, self::$wildcards, true))
323-
$major = 'x';
324-
if (in_array($minor, self::$wildcards, true))
325-
$minor = 'x';
326-
if (in_array($patch, self::$wildcards, true))
327-
$patch = 'x';
328358
}
329359

330360
/**

tests/semver_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function testValidRange() {
253253
$compare=array(
254254
array("1.0.0 - 2.0.0",">=1.0.0 <=2.0.0")
255255
, array("1.0.0","1.0.0")
256-
, array(">=*",">=0")
256+
, array(">=*",">=0.0.0-")
257257
// , array("","")
258258
, array("*",">=0")
259259
, array(">=1.0.0",">=1.0.0")

0 commit comments

Comments
 (0)