Skip to content

Commit

Permalink
Make use of strict types and support v1.2.3-rc format
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed May 5, 2020
1 parent c7409c2 commit ade1055
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
16 changes: 8 additions & 8 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public function getPatch(): VersionNumber {
}

private function parseVersion(array $matches): void {
$this->major = new VersionNumber($matches['Major']);
$this->minor = new VersionNumber($matches['Minor']);
$this->patch = isset($matches['Patch']) ? new VersionNumber($matches['Patch']) : new VersionNumber(null);
$this->major = new VersionNumber((int)$matches['Major']);
$this->minor = new VersionNumber((int)$matches['Minor']);
$this->patch = isset($matches['Patch']) ? new VersionNumber((int)$matches['Patch']) : new VersionNumber(null);

if (isset($matches['PreReleaseSuffix'])) {
$this->preReleaseSuffix = new PreReleaseSuffix($matches['PreReleaseSuffix']);
Expand All @@ -115,17 +115,17 @@ private function parseVersion(array $matches): void {
*/
private function ensureVersionStringIsValid($version): void {
$regex = '/^v?
(?<Major>(0|(?:[1-9][0-9]*)))
(?<Major>(0|(?:[1-9]\d*)))
\\.
(?<Minor>(0|(?:[1-9][0-9]*)))
(?<Minor>(0|(?:[1-9]\d*)))
(\\.
(?<Patch>(0|(?:[1-9][0-9]*)))
(?<Patch>(0|(?:[1-9]\d*)))
)?
(?:
-
(?<PreReleaseSuffix>(?:(dev|beta|b|RC|alpha|a|patch|p)\.?\d*))
(?<PreReleaseSuffix>(?:(dev|beta|b|rc|alpha|a|patch|p)\.?\d*))
)?
$/x';
$/xi';

if (\preg_match($regex, $version, $matches) !== 1) {
throw new InvalidVersionException(
Expand Down
2 changes: 1 addition & 1 deletion src/VersionConstraintParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function parse($value): VersionConstraint {
return $this->handleOrGroup($value);
}

if (!\preg_match('/^[\^~\*]?[\d.\*]+(?:-.*)?$/', $value)) {
if (!\preg_match('/^[\^~*]?[\d.*]+(?:-.*)?$/', $value)) {
throw new UnsupportedVersionConstraintException(
\sprintf('Version constraint %s is not supported.', $value)
);
Expand Down
18 changes: 13 additions & 5 deletions src/VersionConstraintValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ public function getPatch(): VersionNumber {
private function parseVersion($versionString): void {
$this->extractBuildMetaData($versionString);
$this->extractLabel($versionString);
$this->stripPotentialVPrefix($versionString);

$versionSegments = \explode('.', $versionString);
$this->major = new VersionNumber($versionSegments[0]);
$this->major = new VersionNumber(\is_numeric($versionSegments[0]) ? (int)$versionSegments[0] : null);

$minorValue = $versionSegments[1] ?? null;
$patchValue = $versionSegments[2] ?? null;
$minorValue = isset($versionSegments[1]) && \is_numeric($versionSegments[1]) ? (int)$versionSegments[1] : null;
$patchValue = isset($versionSegments[2]) && \is_numeric($versionSegments[2]) ? (int)$versionSegments[2] : null;

$this->minor = new VersionNumber($minorValue);
$this->patch = new VersionNumber($patchValue);
Expand All @@ -74,7 +75,7 @@ private function parseVersion($versionString): void {
* @param string $versionString
*/
private function extractBuildMetaData(&$versionString): void {
if (\preg_match('/\+(.*)/', $versionString, $matches) == 1) {
if (\preg_match('/\+(.*)/', $versionString, $matches) === 1) {
$this->buildMetaData = $matches[1];
$versionString = \str_replace($matches[0], '', $versionString);
}
Expand All @@ -84,9 +85,16 @@ private function extractBuildMetaData(&$versionString): void {
* @param string $versionString
*/
private function extractLabel(&$versionString): void {
if (\preg_match('/\-(.*)/', $versionString, $matches) == 1) {
if (\preg_match('/-(.*)/', $versionString, $matches) === 1) {
$this->label = $matches[1];
$versionString = \str_replace($matches[0], '', $versionString);
}
}

private function stripPotentialVPrefix(&$versionString): void {
if ($versionString[0] !== 'v') {
return;
}
$versionString = \substr($versionString, 1);
}
}
11 changes: 5 additions & 6 deletions src/VersionNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
namespace PharIo\Version;

class VersionNumber {
/** @var int */

/** @var ?int */
private $value;

public function __construct($value) {
if (\is_numeric($value)) {
$this->value = $value;
}
public function __construct(?int $value) {
$this->value = $value;
}

public function isAny(): bool {
return $this->value === null;
}

public function getValue(): int {
public function getValue(): ?int {
return $this->value;
}
}
9 changes: 5 additions & 4 deletions tests/Unit/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public function testParsesVersionNumbers(

public function versionProvider() {
return [
['0.0.1', '0', '0', '1'],
['0.1.2', '0', '1', '2'],
['1.0.0-alpha', '1', '0', '0', 'alpha'],
['3.4.12-dev3', '3', '4', '12', 'dev', 3],
['0.0.1', 0, 0, 1],
['0.1.2', 0, 1, 2],
['1.0.0-alpha', 1, 0, 0, 'alpha'],
['3.4.12-dev3', 3, 4, 12, 'dev', 3],
['v1.2.3-rc', 1, 2, 3, 'rc']
];
}

Expand Down

0 comments on commit ade1055

Please sign in to comment.