-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Fix version comparison algorithm #116
Conversation
We were using a string-based comparison to handle this, which is rather error-prone since it considers '1.10.0' <= '1.9.0'. This makes sure we use integers to perform the comparison in the correct hierarchy of the semver rules. Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Thanks for spotting this one, @lcobucci!
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; |
There was a problem hiding this comment.
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];
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
Description
We were using a string-based comparison to handle this, which is rather error-prone since it considers '1.10.0' <= '1.9.0'.
This makes sure we use integers to perform the comparison in the correct hierarchy of the semver rules.
This seems to be the root cause of #74.