Skip to content

Commit

Permalink
Update SemVer regex and method names to reflect spec
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed May 24, 2024
1 parent 9401ccb commit 047e9c1
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions formwork/src/Utils/SemVer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ final class SemVer implements Stringable
{
/**
* Regex matching version components
*
* @see https://semver.org/
*/
protected const SEMVER_REGEX = '/^(?<major>0|(?:[1-9]\d*))(?:\.(?<minor>0|(?:[1-9]\d*))?(?:\.(?<patch>0|(?:[1-9]\d*)))?(?:\-(?<prerelease>[0-9A-Z\.-]+))?(?:\+(?<metadata>[0-9A-Z\.-]+))?)?$/i';
protected const SEMVER_REGEX = '/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/';

/**
* Valid operators to compare versions
Expand All @@ -22,10 +24,7 @@ final class SemVer implements Stringable
*/
protected const PRERELEASE_TAGS = ['dev', 'alpha', 'beta', 'RC', 'pl'];

/**
* Create a new SemVer instance
*/
public function __construct(protected int $major = 0, protected int $minor = 0, protected int $patch = 0, protected ?string $prerelease = null, protected ?string $metadata = null)
public function __construct(protected int $major = 0, protected int $minor = 0, protected int $patch = 0, protected ?string $prerelease = null, protected ?string $buildMetadata = null)
{
if ($this->major < 0 || $this->minor < 0 || $this->patch < 0) {
throw new InvalidArgumentException('$major, $minor and $patch arguments must be non-negative integers');
Expand All @@ -43,7 +42,7 @@ public function __toString(): string
$this->minor,
$this->patch,
$this->prerelease !== null ? '-' . $this->prerelease : '',
$this->metadata !== null ? '+' . $this->metadata : ''
$this->buildMetadata !== null ? '+' . $this->buildMetadata : ''
);
}

Expand Down Expand Up @@ -76,21 +75,21 @@ public function patch(): int
*/
public function prerelease(): ?string
{
return $this->prerelease();
return $this->prerelease;
}

/**
* Get version version metadata string
* Get version version build metadata string
*/
public function metadata(): ?string
public function buildMetadata(): ?string
{
return $this->metadata();
return $this->buildMetadata;
}

/**
* Return an instance with only major, minor and patch numbers
* Return an instance with only the version core, i.e. `major.minor.patch`
*/
public function onlyNumbers(): self
public function versionCore(): self
{
return new self($this->major, $this->minor, $this->patch);
}
Expand All @@ -108,21 +107,21 @@ public function isPrerelease(): bool
*/
public function withoutPrerelease(): self
{
return new self($this->major, $this->minor, $this->patch, null, $this->metadata);
return new self($this->major, $this->minor, $this->patch, null, $this->buildMetadata);
}

/**
* Return whether the version has metadata
* Return whether the version has build metadata
*/
public function hasMetadata(): bool
public function hasBuildMetadata(): bool
{
return $this->metadata !== null;
return $this->buildMetadata !== null;
}

/**
* Return an instance without version metadata
* Return an instance without version build metadata
*/
public function withoutMetadata(): self
public function withoutBuildMetadata(): self
{
return new self($this->major, $this->minor, $this->patch, $this->prerelease);
}
Expand Down Expand Up @@ -156,7 +155,7 @@ public function nextPatch(): self
*/
public function toComparableString(): string
{
return (string) $this->withoutMetadata();
return (string) $this->withoutBuildMetadata();
}

/**
Expand All @@ -168,10 +167,10 @@ public function compareWith(self $version, string $operator): bool
throw new InvalidArgumentException(sprintf('Invalid operator for version comparison: "%s". Use one of the following: "%s"', $operator, implode('", "', self::COMPARISON_OPERATORS)));
}
if ($operator === '~') {
return $this->compareWith($version, '<=') && $this->nextMinor()->compareWith($version->onlyNumbers(), '>');
return $this->compareWith($version, '<=') && $this->nextMinor()->compareWith($version->versionCore(), '>');
}
if ($operator === '^') {
return $this->compareWith($version, '<=') && $this->nextMajor()->compareWith($version->onlyNumbers(), '>');
return $this->compareWith($version, '<=') && $this->nextMajor()->compareWith($version->versionCore(), '>');
}
return version_compare($this->toComparableString(), $version->toComparableString(), $operator);
}
Expand All @@ -192,7 +191,7 @@ public static function fromString(string $version): self
if (!preg_match(self::SEMVER_REGEX, $version, $matches, PREG_UNMATCHED_AS_NULL)) {
throw new InvalidArgumentException(sprintf('Invalid version string: "%s"', $version));
}
return new self((int) ($matches['major']), (int) ($matches['minor']), (int) ($matches['patch']), $matches['prerelease'], $matches['metadata']);
return new self((int) ($matches['major']), (int) ($matches['minor']), (int) ($matches['patch']), $matches['prerelease'], $matches['buildmetadata']);
}

/**
Expand Down

0 comments on commit 047e9c1

Please sign in to comment.