Skip to content

Commit c458d45

Browse files
committed
Used named capture groups in semver regular expression
1 parent d67885a commit c458d45

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/Version.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ public function __toString() : string
7171
*/
7272
public function setVersion(string $version) : self
7373
{
74-
$semverRegex = '/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-.]+)?)?$/';
74+
$semverRegex = '/^v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<pre_release>[0-9A-Za-z-.]+))?(?:\+(?<build>[0-9A-Za-z-.]+)?)?$/';
7575

7676
if (! preg_match($semverRegex, $version, $matches)) {
7777
throw new InvalidVersionException('Invalid Semantic Version string provided');
7878
}
7979

80-
$this->major = (int) $matches[1];
81-
$this->minor = (int) $matches[2];
82-
$this->patch = (int) $matches[3];
83-
$this->preRelease = $matches[4] ?? null;
84-
$this->build = $matches[5] ?? null;
80+
$this->major = (int) $matches['major'];
81+
$this->minor = (int) $matches['minor'];
82+
$this->patch = (int) $matches['patch'];
83+
$this->preRelease = $matches['pre_release'] ?? null;
84+
$this->build = $matches['build'] ?? null;
8585

8686
return $this;
8787
}

0 commit comments

Comments
 (0)