Skip to content

Commit 815fe51

Browse files
authored
Merge pull request #212 from mpyw/refactor/use-composer-semver
Refactor: Use composer/semver instead of custom ComposerVersion
2 parents cb61c4a + 1be74f9 commit 815fe51

File tree

8 files changed

+141
-112
lines changed

8 files changed

+141
-112
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"require": {
1414
"php": "^8.1",
1515
"ext-json": "*",
16+
"composer/semver": "^3.4",
1617
"guzzlehttp/guzzle": "^7.7",
1718
"jetbrains/phpstorm-attributes": "^1.0",
1819
"mschop/pathogen": "^0.7.1",

composer.lock

Lines changed: 80 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/versioning.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
services:
2-
Siketyan\Loxcan\Versioning\Composer\ComposerVersionParser: ~
2+
Composer\Semver\VersionParser: ~
3+
4+
Siketyan\Loxcan\Versioning\Composer\ComposerVersionParser:
5+
arguments:
6+
$parser: '@Composer\Semver\VersionParser'
37

48
Siketyan\Loxcan\Versioning\Composer\ComposerVersionComparator: ~
59

src/Versioning/Composer/ComposerVersion.php

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,53 @@ class ComposerVersion implements VersionInterface, CompatibilityAwareInterface,
1212
{
1313
use HasSemVerLikeCompatibility;
1414

15-
public const STABILITIES = [
16-
'dev' => self::STABILITY_DEV,
17-
'alpha' => self::STABILITY_ALPHA,
18-
'beta' => self::STABILITY_BETA,
19-
'RC' => self::STABILITY_RC,
20-
'stable' => self::STABILITY_STABLE,
21-
];
22-
23-
public const STABILITY_DEV = 0;
24-
public const STABILITY_ALPHA = 10;
25-
public const STABILITY_BETA = 20;
26-
public const STABILITY_RC = 30;
27-
public const STABILITY_STABLE = 100;
28-
2915
public function __construct(
30-
private readonly int $x,
31-
private readonly int $y,
32-
private readonly int $z,
33-
private readonly int $stability = self::STABILITY_STABLE,
34-
private readonly int $number = 0,
16+
private readonly string $normalized,
17+
private readonly string $pretty,
18+
private readonly int $major,
19+
private readonly int $minor,
20+
private readonly int $patch,
3521
private readonly string $hash = '',
3622
private readonly ?string $branch = null,
3723
) {
3824
}
3925

4026
public function __toString(): string
4127
{
42-
if ($this->branch) {
28+
if ($this->branch !== null) {
4329
return sprintf(
4430
'%s@%s',
4531
$this->branch,
4632
$this->hash,
4733
);
4834
}
4935

50-
if ($this->stability < self::STABILITY_STABLE) {
51-
return sprintf(
52-
'v%d.%d.%d-%s%d',
53-
$this->x,
54-
$this->y,
55-
$this->z,
56-
array_flip(self::STABILITIES)[$this->stability],
57-
$this->number,
58-
);
59-
}
60-
61-
return sprintf(
62-
'v%d.%d.%d',
63-
$this->x,
64-
$this->y,
65-
$this->z,
66-
);
36+
return $this->pretty;
6737
}
6838

69-
public function getX(): int
39+
public function getNormalized(): string
7040
{
71-
return $this->x;
41+
return $this->normalized;
7242
}
7343

74-
public function getY(): int
44+
public function getPretty(): string
7545
{
76-
return $this->y;
46+
return $this->pretty;
7747
}
7848

79-
public function getZ(): int
49+
public function getX(): int
8050
{
81-
return $this->z;
51+
return $this->major;
8252
}
8353

84-
public function getStability(): int
54+
public function getY(): int
8555
{
86-
return $this->stability;
56+
return $this->minor;
8757
}
8858

89-
public function getNumber(): int
59+
public function getZ(): int
9060
{
91-
return $this->number;
61+
return $this->patch;
9262
}
9363

9464
public function getHash(): string

src/Versioning/Composer/ComposerVersionComparator.php

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Siketyan\Loxcan\Versioning\Composer;
66

7-
use JetBrains\PhpStorm\Pure;
7+
use Composer\Semver\Comparator;
88
use Siketyan\Loxcan\Exception\RuntimeException;
99
use Siketyan\Loxcan\Versioning\VersionComparatorInterface;
1010
use Siketyan\Loxcan\Versioning\VersionDiff;
@@ -42,46 +42,16 @@ public function supports(string $beforeType, string $afterType): bool
4242
return $beforeType === ComposerVersion::class && $afterType === ComposerVersion::class;
4343
}
4444

45-
#[Pure]
4645
private function determineType(ComposerVersion $before, ComposerVersion $after): ?int
4746
{
48-
if ($before->getX() < $after->getX()) {
49-
return VersionDiff::UPGRADED;
50-
}
51-
52-
if ($before->getX() > $after->getX()) {
53-
return VersionDiff::DOWNGRADED;
54-
}
55-
56-
if ($before->getY() < $after->getY()) {
57-
return VersionDiff::UPGRADED;
58-
}
59-
60-
if ($before->getY() > $after->getY()) {
61-
return VersionDiff::DOWNGRADED;
62-
}
63-
64-
if ($before->getZ() < $after->getZ()) {
65-
return VersionDiff::UPGRADED;
66-
}
67-
68-
if ($before->getZ() > $after->getZ()) {
69-
return VersionDiff::DOWNGRADED;
70-
}
71-
72-
if ($before->getStability() < $after->getStability()) {
73-
return VersionDiff::UPGRADED;
74-
}
75-
76-
if ($before->getStability() > $after->getStability()) {
77-
return VersionDiff::DOWNGRADED;
78-
}
47+
$beforeNormalized = $before->getNormalized();
48+
$afterNormalized = $after->getNormalized();
7949

80-
if ($before->getNumber() < $after->getNumber()) {
50+
if (Comparator::lessThan($beforeNormalized, $afterNormalized)) {
8151
return VersionDiff::UPGRADED;
8252
}
8353

84-
if ($before->getNumber() > $after->getNumber()) {
54+
if (Comparator::greaterThan($beforeNormalized, $afterNormalized)) {
8555
return VersionDiff::DOWNGRADED;
8656
}
8757

src/Versioning/Composer/ComposerVersionParser.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,44 @@
44

55
namespace Siketyan\Loxcan\Versioning\Composer;
66

7+
use Composer\Semver\VersionParser as SemverVersionParser;
78
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;
89

910
class ComposerVersionParser
1011
{
11-
private const PATTERN = '/^v?(\d+)\.(\d+)\.(\d+)(?:-(dev|alpha|beta|RC)(\d+)?)?$/';
12+
public function __construct(
13+
private readonly SemverVersionParser $parser,
14+
) {
15+
}
1216

1317
public function parse(string $version, string $hash): ComposerVersion|UnknownVersion
1418
{
1519
if (str_starts_with($version, 'dev-')) {
1620
return new ComposerVersion(
21+
'dev-' . $hash,
22+
$version,
1723
0,
1824
0,
1925
0,
20-
ComposerVersion::STABILITY_DEV,
21-
0,
2226
$hash,
2327
$version,
2428
);
2529
}
2630

27-
if (!preg_match(self::PATTERN, $version, $matches)) {
31+
try {
32+
$normalized = $this->parser->normalize($version);
33+
} catch (\UnexpectedValueException) {
2834
return new UnknownVersion($version);
2935
}
3036

37+
$parts = explode('.', explode('-', $normalized)[0]);
38+
3139
return new ComposerVersion(
32-
(int) $matches[1],
33-
(int) $matches[2],
34-
(int) $matches[3],
35-
\count($matches) > 4 ? ComposerVersion::STABILITIES[$matches[4]] : ComposerVersion::STABILITY_STABLE,
36-
\count($matches) > 5 ? (int) $matches[5] : 0,
40+
$normalized,
41+
$version,
42+
(int) ($parts[0] ?? 0),
43+
(int) ($parts[1] ?? 0),
44+
(int) ($parts[2] ?? 0),
3745
$hash,
3846
);
3947
}

tests/Versioning/Composer/ComposerVersionComparatorTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ public function test(): void
2020
{
2121
$this->assertCompare(
2222
VersionDiff::UPGRADED,
23-
new ComposerVersion(1, 2, 3),
24-
new ComposerVersion(2, 0, 0),
23+
new ComposerVersion('1.2.3.0', 'v1.2.3', 1, 2, 3),
24+
new ComposerVersion('2.0.0.0', 'v2.0.0', 2, 0, 0),
2525
);
2626

2727
$this->assertCompare(
2828
VersionDiff::DOWNGRADED,
29-
new ComposerVersion(4, 3, 2, ComposerVersion::STABILITY_DEV),
30-
new ComposerVersion(3, 4, 5, ComposerVersion::STABILITY_DEV),
29+
new ComposerVersion('4.3.2.0-dev', 'v4.3.2-dev', 4, 3, 2),
30+
new ComposerVersion('3.4.5.0-dev', 'v3.4.5-dev', 3, 4, 5),
3131
);
3232

3333
$this->assertCompare(
3434
VersionDiff::UPGRADED,
35-
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_BETA),
36-
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_RC),
35+
new ComposerVersion('1.2.3.0-beta', 'v1.2.3-beta', 1, 2, 3),
36+
new ComposerVersion('1.2.3.0-RC', 'v1.2.3-RC', 1, 2, 3),
3737
);
3838

3939
$this->assertCompare(
4040
VersionDiff::CHANGED,
41-
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_STABLE, 0, 'hash'),
42-
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_STABLE, 0, 'hash_changed'),
41+
new ComposerVersion('1.2.3.0', 'v1.2.3', 1, 2, 3, 'hash'),
42+
new ComposerVersion('1.2.3.0', 'v1.2.3', 1, 2, 3, 'hash_changed'),
4343
);
4444

4545
$this->assertCompare(
4646
null,
47-
new ComposerVersion(2, 3, 4),
48-
new ComposerVersion(2, 3, 4),
47+
new ComposerVersion('2.3.4.0', 'v2.3.4', 2, 3, 4),
48+
new ComposerVersion('2.3.4.0', 'v2.3.4', 2, 3, 4),
4949
);
5050
}
5151

0 commit comments

Comments
 (0)