Skip to content

Commit

Permalink
Merge pull request #76 from siketyan/versioning/hash-changes
Browse files Browse the repository at this point in the history
✨ Improve comparison between versions
  • Loading branch information
siketyan authored Dec 21, 2021
2 parents 7b0b99a + 8c6b381 commit 1fc0965
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/Command/ScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ private function getVersionDiffTypeEmoji(VersionDiff $diff): string
case VersionDiff::DOWNGRADED:
return '⬇️';

case VersionDiff::CHANGED:
return '💥';

default:
case VersionDiff::UNKNOWN:
return '🔄';
Expand Down
21 changes: 10 additions & 11 deletions src/Comparator/DependencyComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Siketyan\Loxcan\Model\Dependency;
use Siketyan\Loxcan\Model\DependencyDiff;
use Siketyan\Loxcan\Versioning\VersionComparatorResolver;
use Siketyan\Loxcan\Versioning\VersionDiff;

class DependencyComparator
{
Expand All @@ -33,20 +34,18 @@ public function compare(Dependency $before, Dependency $after): ?DependencyDiff
);

if ($versionComparator === null) {
throw new InvalidComparisonException(
sprintf(
'No comparator supports to compare versions between "%s" and "%s".',
get_debug_type($before->getVersion()),
get_debug_type($after->getVersion()),
),
$versionDiff = new VersionDiff(
VersionDiff::UNKNOWN,
$before->getVersion(),
$after->getVersion(),
);
} else {
$versionDiff = $versionComparator->compare(
$before->getVersion(),
$after->getVersion(),
);
}

$versionDiff = $versionComparator->compare(
$before->getVersion(),
$after->getVersion(),
);

if ($versionDiff === null) {
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Reporter/GitHub/GitHubMarkdownBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ private function getVersionDiffTypeEmoji(VersionDiff $diff): string
case VersionDiff::DOWNGRADED:
return '⬇️';

case VersionDiff::CHANGED:
return '💥';

default:
case VersionDiff::UNKNOWN:
return '🔄';
Expand Down
3 changes: 2 additions & 1 deletion src/Scanner/Composer/ComposerLockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function parse(?string $json): DependencyCollection
foreach ($packages as $package) {
$name = $package['name'];
$version = $package['version'];
$hash = $package['source']['reference'];
$package = $this->packagePool->get($name);

if ($package === null) {
Expand All @@ -59,7 +60,7 @@ public function parse(?string $json): DependencyCollection

$dependencies[] = new Dependency(
$package,
$this->versionParser->parse($version),
$this->versionParser->parse($version, $hash),
);
}

Expand Down
26 changes: 25 additions & 1 deletion src/Versioning/Composer/ComposerVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,37 @@ class ComposerVersion implements VersionInterface
private int $z;
private int $stability;
private int $number;
private string $hash;
private ?string $branch;

public function __construct(
int $x,
int $y,
int $z,
int $stability = self::STABILITY_STABLE,
int $number = 0
int $number = 0,
string $hash = "",
?string $branch = null
) {
$this->x = $x;
$this->y = $y;
$this->z = $z;
$this->stability = $stability;
$this->number = $number;
$this->hash = $hash;
$this->branch = $branch;
}

public function __toString(): string
{
if ($this->branch) {
return sprintf(
'%s@%s',
$this->branch,
$this->hash,
);
}

if ($this->stability < self::STABILITY_STABLE) {
return sprintf(
'v%d.%d.%d-%s%d',
Expand Down Expand Up @@ -87,4 +101,14 @@ public function getNumber(): int
{
return $this->number;
}

public function getHash(): string
{
return $this->hash;
}

public function getBranch(): ?string
{
return $this->branch;
}
}
8 changes: 8 additions & 0 deletions src/Versioning/Composer/ComposerVersionComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function supports(string $beforeType, string $afterType): bool

private function determineType(ComposerVersion $before, ComposerVersion $after): ?int
{
if ($before->getHash() !== $after->getHash()) {
return VersionDiff::CHANGED;
}

if ($before->getBranch() !== $after->getBranch()) {
return VersionDiff::UNKNOWN;
}

if ($before->getX() < $after->getX()) {
return VersionDiff::UPGRADED;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Versioning/Composer/ComposerVersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ class ComposerVersionParser
{
private const PATTERN = '/^v?(\d+)\.(\d+)\.(\d+)(?:-(dev|alpha|beta|RC)(\d+)?)?$/';

public function parse(string $version): VersionInterface
public function parse(string $version, string $hash): VersionInterface
{
if (str_starts_with($version, 'dev-')) {
return new ComposerVersion(
0,
0,
0,
ComposerVersion::STABILITY_DEV,
0,
$hash,
$version,
);
}

if (!preg_match(self::PATTERN, $version, $matches)) {
return new UnknownVersion($version);
}
Expand All @@ -23,6 +35,7 @@ public function parse(string $version): VersionInterface
(int) $matches[3],
count($matches) > 4 ? ComposerVersion::STABILITIES[$matches[4]] : ComposerVersion::STABILITY_STABLE,
count($matches) > 5 ? (int) $matches[5] : 0,
$hash,
);
}
}
8 changes: 5 additions & 3 deletions src/Versioning/Unknown/UnknownVersionComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class UnknownVersionComparator implements VersionComparatorInterface
{
public function compare(VersionInterface $before, VersionInterface $after): ?VersionDiff
{
if (((string) $before) === ((string) $after)) {
return null;
}

return new VersionDiff(
VersionDiff::UNKNOWN,
$before,
Expand All @@ -21,8 +25,6 @@ public function compare(VersionInterface $before, VersionInterface $after): ?Ver

public function supports(string $beforeType, string $afterType): bool
{
return is_a($beforeType, VersionInterface::class, true)
&& is_a($afterType, VersionInterface::class, true)
;
return $beforeType === UnknownVersion::class && $afterType === UnknownVersion::class;
}
}
1 change: 1 addition & 0 deletions src/Versioning/VersionDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class VersionDiff
{
public const UPGRADED = 1;
public const DOWNGRADED = -1;
public const CHANGED = 9;
public const UNKNOWN = 0;

private int $type;
Expand Down
14 changes: 10 additions & 4 deletions tests/Scanner/Composer/ComposerLockParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ class ComposerLockParserTest extends TestCase
"packages": [
{
"name": "foo/bar",
"version": "v1.2.3.4"
"version": "v1.2.3.4",
"source": {
"reference": "hash"
}
}
],
"packages-dev": [
{
"name": "bar/baz",
"version": "3.2.1"
"version": "3.2.1",
"source": {
"reference": "hash"
}
}
]
}
Expand Down Expand Up @@ -59,8 +65,8 @@ public function test(): void
$this->packagePool->get('bar/baz')->willReturn($cache);
$this->packagePool->add(Argument::type(Package::class))->shouldBeCalledOnce();

$this->versionParser->parse('v1.2.3.4')->willReturn($fooBarVersion);
$this->versionParser->parse('3.2.1')->willReturn($barBazVersion);
$this->versionParser->parse('v1.2.3.4', 'hash')->willReturn($fooBarVersion);
$this->versionParser->parse('3.2.1', 'hash')->willReturn($barBazVersion);

$collection = $this->parser->parse(self::CONTENTS);
$dependencies = $collection->getDependencies();
Expand Down
6 changes: 6 additions & 0 deletions tests/Versioning/Composer/ComposerVersionComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function test(): void
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_RC),
);

$this->assertCompare(
VersionDiff::CHANGED,
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_STABLE, 0, 'hash'),
new ComposerVersion(1, 2, 3, ComposerVersion::STABILITY_STABLE, 0, 'hash_changed'),
);

$this->assertCompare(
null,
new ComposerVersion(2, 3, 4),
Expand Down
15 changes: 12 additions & 3 deletions tests/Versioning/Composer/ComposerVersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,35 @@ protected function setUp(): void

public function test(): void
{
$version = $this->parser->parse('v1.2.3-dev123');
/* @var ComposerVersion $version */
$version = $this->parser->parse('v1.2.3-dev123', 'hash');
$this->assertSame(1, $version->getX());
$this->assertSame(2, $version->getY());
$this->assertSame(3, $version->getZ());
$this->assertSame(ComposerVersion::STABILITY_DEV, $version->getStability());
$this->assertSame(123, $version->getNumber());
$this->assertSame('hash', $version->getHash());

$version = $this->parser->parse('4.3.2');
/* @var ComposerVersion $version */
$version = $this->parser->parse('4.3.2', 'hash');
$this->assertSame(4, $version->getX());
$this->assertSame(3, $version->getY());
$this->assertSame(2, $version->getZ());
$this->assertSame(ComposerVersion::STABILITY_STABLE, $version->getStability());
$this->assertSame(0, $version->getNumber());
$this->assertSame('hash', $version->getHash());

/* @var ComposerVersion $version */
$version = $this->parser->parse('dev-branch', 'hash');
$this->assertSame('dev-branch', $version->getBranch());
$this->assertSame('hash', $version->getHash());
}

public function testUnsupported(): void
{
$this->assertInstanceOf(
UnknownVersion::class,
$this->parser->parse('v1.2.3_not_supported'),
$this->parser->parse('v1.2.3_not_supported', 'hash'),
);
}
}
4 changes: 2 additions & 2 deletions tests/Versioning/Unknown/UnknownVersionComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public function testSupports(): void
$this->comparator->supports(UnknownVersion::class, UnknownVersion::class),
);

$this->assertTrue(
$this->assertFalse(
$this->comparator->supports(SimpleVersion::class, UnknownVersion::class),
);

$this->assertTrue(
$this->assertFalse(
$this->comparator->supports(SemVerVersion::class, SimpleVersion::class),
);

Expand Down

0 comments on commit 1fc0965

Please sign in to comment.