-
Notifications
You must be signed in to change notification settings - Fork 28
Update package to use Composer\Semver and more robust version sorting/comparing #34
base: master
Are you sure you want to change the base?
Changes from 3 commits
bdaaa86
2d75629
c24dae9
b359561
64d0232
a4d1d9c
479065a
7feead0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,9 @@ | |
|
|
||
| namespace Humbug\SelfUpdate; | ||
|
|
||
| use Composer\Semver\Semver; | ||
| use Composer\Semver\VersionParser as Parser; | ||
|
|
||
| class VersionParser | ||
| { | ||
|
|
||
|
|
@@ -20,17 +23,23 @@ class VersionParser | |
| */ | ||
| private $versions; | ||
|
|
||
| /** | ||
| * @var Composer\VersionParser | ||
| */ | ||
| private $parser; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $modifier = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; | ||
| const GIT_DATA_MATCH = '/.*(-\d+-g[[:alnum:]]{7})$/'; | ||
|
|
||
| /** | ||
| * @param array $versions | ||
| */ | ||
| public function __construct(array $versions = array()) | ||
| { | ||
| $this->versions = $versions; | ||
| $this->parser = new Parser; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend not omitting
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not part of the PSR-2 standard which is enforced. We might adopt PSR-12 one day, if it ever leaves draft to get voted on, but I won't expect any contributor to follow a standard which we don't advertise/enforce upfront. That includes me and my own bias towards wondering why the seven hells I need to include parentheses when there are zero parameters ;).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember the first time I saw this I was confused as I didn't know it was possible, and when you call a function In any case, it's my own bias as well and I don't think anyone would feel strongly about it 😄
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's why I don't like PSR-2 - too much room for improvement. Anyway please make it consistent across the code. If in all other places the |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -112,6 +121,20 @@ public function isDevelopment($version) | |
| return $this->development($version); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if two version strings are the same normalised version. | ||
| * | ||
| * @param string | ||
| * @param string | ||
| * @return bool | ||
| */ | ||
| public static function equals($version1, $version2) | ||
| { | ||
| $parser = new Parser; | ||
| return $parser->normalize(self::stripGitHash($version1)) | ||
| === $parser->normalize(self::stripGitHash($version2)); | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| private function selectRecentStable() | ||
| { | ||
| $candidates = array(); | ||
|
|
@@ -159,44 +182,31 @@ private function selectRecentAll() | |
|
|
||
| private function findMostRecent(array $candidates) | ||
| { | ||
| $candidate = null; | ||
| $tracker = null; | ||
| foreach ($candidates as $version) { | ||
| if (version_compare($candidate, $version, '<')) { | ||
| $candidate = $version; | ||
| } | ||
| } | ||
| return $candidate; | ||
| $sorted = Semver::rsort($candidates); | ||
| return $sorted[0]; | ||
| } | ||
|
|
||
| private function stable($version) | ||
| { | ||
| $version = preg_replace('{#.+$}i', '', $version); | ||
| if ($this->development($version)) { | ||
| return false; | ||
| } | ||
| preg_match('{'.$this->modifier.'$}i', strtolower($version), $match); | ||
| if (!empty($match[3])) { | ||
| return false; | ||
| } | ||
| if (!empty($match[1])) { | ||
| if ('beta' === $match[1] || 'b' === $match[1] | ||
| || 'alpha' === $match[1] || 'a' === $match[1] | ||
| || 'rc' === $match[1]) { | ||
| return false; | ||
| } | ||
| if ('stable' === Parser::parseStability(self::stripGitHash($version))) { | ||
| return true; | ||
| } | ||
| return true; | ||
| return false; | ||
| } | ||
|
|
||
| private function development($version) | ||
| { | ||
| if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) { | ||
| return true; | ||
| } | ||
| if (1 == preg_match("/-\d+-[a-z0-9]{8,}$/", $version)) { | ||
| if ('dev' === Parser::parseStability(self::stripGitHash($version))) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static function stripGitHash($version) | ||
| { | ||
| if (preg_match(self::GIT_DATA_MATCH, $version, $matches)) { | ||
| $version = str_replace($matches[1], '', $version); | ||
| } | ||
| return $version; | ||
| } | ||
| } | ||
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.
UnexpectedValueException thrown by Composer\Semver. I should add a check that we do the following
!==comparison only if the strategy in play is NOT github. Otherwise, we'll silently fail on invalid semver when there MUST be a valid semver version string.