Skip to content

Fix version comparison algorithm #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Git/Value/SemVerVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public function isNewMajorRelease(): bool

public function lessThanEqual(self $other): bool
{
return $this->fullReleaseName() <= $other->fullReleaseName();
return $this->compare($other) <= 0;
}

private function compare(self $other): int
{
$comparison = $this->major <=> $other->major;

if ($comparison !== 0) {
return $comparison;
}

$comparison = $this->minor <=> $other->minor;

return $comparison !== 0 ? $comparison : $this->patch <=> $other->patch;
Comment on lines +92 to +102
Copy link
Contributor

@bendavies bendavies Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can all be condensed to:

return [$this->major, $this->minor, $this->patch] <=> [$other->major, $other->minor, $other->patch];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bendavies cool, didn't know. Would you like to send a patch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use something like:

it's been battle-tested since 2013 or so being part of the composer package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be done, but this code was supposed to work on a very isolated scenario, so IMO, since it is tested and relatively well protected from mutants, introducing a dependency here will likely raise more issues due to the use-case scenario being widened (while it is intentionally narrow)

}
}
6 changes: 6 additions & 0 deletions test/unit/Git/Value/SemVerVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ public function lessThanEqualProvider(): array
['0.1.1', '0.1.0', false],
['2.0.0', '1.0.0', false],
['1.0.0', '2.0.0', true],
['1.10.0', '1.9.0', false],
['1.9.0', '1.10.0', true],
['1.0.10', '1.0.9', false],
['1.0.9', '1.0.10', true],
['10.0.0', '9.0.0', false],
['9.0.0', '10.0.0', true],
];
}

Expand Down