Skip to content

Commit 7f0a0cf

Browse files
committed
Improved build number generation, added comparison methods.
The previous system did not allow the correct sorting of versions. An alpha version should always be considered lower than the same version that is not an alpha. This fixes the discrepancy.
1 parent 9d56259 commit 7f0a0cf

1 file changed

Lines changed: 43 additions & 13 deletions

File tree

src/VersionParser.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function getBuildNumber() : float
134134
*/
135135
public function getBuildNumberInt() : int
136136
{
137-
return intval($this->buildNumber * 1000000);
137+
return intval($this->getBuildNumber() * 1000000);
138138
}
139139

140140
/**
@@ -166,20 +166,22 @@ public function getPatchVersion() : int
166166
{
167167
return $this->parts[2];
168168
}
169-
170-
/**
171-
* Retrieves the full version with tag appended, if any.
172-
*
173-
* @return string
174-
*/
169+
170+
/**
171+
* Retrieves the full version with tag appended, if any.
172+
*
173+
* @return string
174+
*/
175175
public function getTagVersion() : string
176176
{
177+
$version = $this->getVersion();
178+
177179
if(!$this->hasTag())
178180
{
179-
return $this->getVersion();
181+
return $version;
180182
}
181183

182-
return $this->getVersion().'-'.$this->getTag();
184+
return $version.'-'.$this->getTag();
183185
}
184186

185187
/**
@@ -319,6 +321,7 @@ private function parse() : void
319321
private function extractTag() : string
320322
{
321323
$version = $this->version;
324+
$version = str_replace('_', '-', $version);
322325

323326
$hyphen = strpos($version, '-');
324327

@@ -361,11 +364,16 @@ private function normalizeTag() : string
361364

362365
private function formatTagNumber() : string
363366
{
367+
$positions = 2 * 3;
364368
$weight = $this->tagWeights[$this->getTagType()];
365369

366370
if($weight > 0)
367371
{
368-
return '.'.sprintf('%0'.$weight.'d', $this->tagNumber);
372+
$number = sprintf('%0'.$weight.'d', $this->tagNumber);
373+
$number = str_pad($number, $positions, '0', STR_PAD_RIGHT);
374+
375+
$number = intval(str_repeat('9', $positions)) - intval($number);
376+
return '.'.$number;
369377
}
370378

371379
return '';
@@ -438,14 +446,36 @@ private function calculateBuildNumber() : void
438446
sprintf('%03d', $this->getPatchVersion())
439447
);
440448

441-
$number = implode('', $parts);
449+
$number = floatval(implode('', $parts));
442450

443451
if($this->tagNumber > 0)
444452
{
445-
$number .= $this->formatTagNumber();
453+
$number -= floatval($this->formatTagNumber());
446454
}
447455

448-
$this->buildNumber = floatval($number);
456+
$this->buildNumber = $number;
457+
}
458+
459+
/**
460+
* Whether this version is higher than the specified version.
461+
*
462+
* @param VersionParser $version
463+
* @return bool
464+
*/
465+
public function isHigherThan(VersionParser $version) : bool
466+
{
467+
return $this->getBuildNumberInt() > $version->getBuildNumberInt();
468+
}
469+
470+
/**
471+
* Whether this version is lower than the specified version.
472+
*
473+
* @param VersionParser $version
474+
* @return bool
475+
*/
476+
public function isLowerThan(VersionParser $version) : bool
477+
{
478+
return $this->getBuildNumberInt() < $version->getBuildNumberInt();
449479
}
450480
}
451481

0 commit comments

Comments
 (0)