Skip to content
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

Sort version numbers in ascending order #816

Merged
merged 3 commits into from
Feb 21, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ For a full diff see [`3.0.0...main`][3.0.0...main].
- Adjusted `Vendor\Composer\ComposerJsonNormalizer` to reject JSON when it is not an object ([#804]), by [@localheinz]
- Adjusted `Vendor\Composer\ComposerJsonNormalizer` to compose `WithFinalNewLineNormalizer` ([#806]), by [@localheinz]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to skip normalization of version constraints when they can not be parsed by `Composer\Semver\VersionParser` ([#813]), by [@fredden] and [@localheinz]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to sort versions in ascending order ([#816]), by [@fredden]
- Adjusted `Vendor\Composer\VersionConstraintNormalizer` to normalize version constraints separators in `and` constraints from space (` `) or comma (`,`) to space (` `) ([#819]), by [@fredden]

### Fixed
Expand Down Expand Up @@ -560,6 +561,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#804]: https://github.com/ergebnis/json-normalizer/pull/804
[#805]: https://github.com/ergebnis/json-normalizer/pull/805
[#813]: https://github.com/ergebnis/json-normalizer/pull/813
[#816]: https://github.com/ergebnis/json-normalizer/pull/816
[#819]: https://github.com/ergebnis/json-normalizer/pull/819

[@BackEndTea]: https://github.com/BackEndTea
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,17 @@ sections, the `Vendor\Composer\VersionConstraintNormalizer` will ensure that
}
```

- version numbers are sorted in ascending order

```diff
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"require": {
- "foo/bar": "^2.0 || ^1.4"
+ "foo/bar": "^1.4 || ^2.0"
}
```

:bulb: Find out more about version constraints at [Composer: Version and Constraints](https://getcomposer.org/doc/articles/versions.md).

## Changelog
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"justinrainbow/json-schema": "^5.2.12"
},
"require-dev": {
"composer/semver": "^3.0.0",
"composer/semver": "^3.2.1",
"ergebnis/data-provider": "^1.3.0",
"ergebnis/license": "^2.1.0",
"ergebnis/php-cs-fixer-config": "^5.3.1",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion src/Vendor/Composer/VersionConstraintNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,46 @@ private function normalizeVersionConstraint(string $versionConstraint): string
);
}

return \trim($normalized);
// Sort
$sorter = static function (string $a, string $b): int {
$a = \trim($a, '<>=!~^');
$b = \trim($b, '<>=!~^');

return \strcmp($a, $b);
};

$orGroups = \explode(' || ', $normalized);

foreach ($orGroups as &$or) {
$ranges = \explode(' - ', $or);

foreach ($ranges as &$range) {
if (\str_contains($range, ' as ')) {
$andGroups = [];
$temp = \explode(' ', $range);

while (!empty($temp)) {
if ('as' === $temp[0]) {
\array_shift($temp);
$andGroups[\count($andGroups) - 1] .= ' as ' . \array_shift($temp);
} else {
$andGroups[] = \array_shift($temp);
}
}
} else {
$andGroups = \explode(' ', $range);
}

\usort($andGroups, $sorter);
$range = \implode(' ', $andGroups);
}

\usort($ranges, $sorter);
$or = \implode(' - ', $ranges);
}

\usort($orGroups, $sorter);

return \implode(' || ', $orGroups);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-and-comma-exact-version-unsorted/01-without-spaces-trimmed": "2.3.4 1.2 0",
"combination-and-comma-exact-version-unsorted/02-without-spaces-untrimmed": "2.3.4 1.2 0",
"combination-and-comma-exact-version-unsorted/03-with-space-single-trimmed": "2.3.4 1.2 0",
"combination-and-comma-exact-version-unsorted/04-with-space-single-untrimmed": "2.3.4 1.2 0",
"combination-and-comma-exact-version-unsorted/05-with-space-double-trimmed": "2.3.4 1.2 0",
"combination-and-comma-exact-version-unsorted/06-with-space-double-untrimmed": "2.3.4 1.2 0"
"combination-and-comma-exact-version-unsorted/01-without-spaces-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-unsorted/02-without-spaces-untrimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-unsorted/03-with-space-single-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-unsorted/04-with-space-single-untrimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-unsorted/05-with-space-double-trimmed": "0 1.2 2.3.4",
"combination-and-comma-exact-version-unsorted/06-with-space-double-untrimmed": "0 1.2 2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-and-space-exact-version-unsorted/01-with-space-single-trimmed": "2.3.4 1.2 0",
"combination-and-space-exact-version-unsorted/02-with-space-single-untrimmed": "2.3.4 1.2 0",
"combination-and-space-exact-version-unsorted/03-with-space-double-trimmed": "2.3.4 1.2 0",
"combination-and-space-exact-version-unsorted/04-with-space-double-untrimmed": "2.3.4 1.2 0"
"combination-and-space-exact-version-unsorted/01-with-space-single-trimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-unsorted/02-with-space-single-untrimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-unsorted/03-with-space-double-trimmed": "0 1.2 2.3.4",
"combination-and-space-exact-version-unsorted/04-with-space-double-untrimmed": "0 1.2 2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-or-exact-version-unsorted/01-without-spaces-trimmed": "2.3.4 || 1.2 || 0",
"combination-or-exact-version-unsorted/02-without-spaces-untrimmed": "2.3.4 || 1.2 || 0",
"combination-or-exact-version-unsorted/03-with-single-space-trimmed": "2.3.4 || 1.2 || 0",
"combination-or-exact-version-unsorted/04-with-single-space-untrimmed": "2.3.4 || 1.2 || 0",
"combination-or-exact-version-unsorted/05-with-double-space-trimmed": "2.3.4 || 1.2 || 0",
"combination-or-exact-version-unsorted/06-with-double-space-untrimmed": "2.3.4 || 1.2 || 0"
"combination-or-exact-version-unsorted/01-without-spaces-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-unsorted/02-without-spaces-untrimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-unsorted/03-with-single-space-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-unsorted/04-with-single-space-untrimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-unsorted/05-with-double-space-trimmed": "0 || 1.2 || 2.3.4",
"combination-or-exact-version-unsorted/06-with-double-space-untrimmed": "0 || 1.2 || 2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-or-version-range-caret-unsorted/01-without-spaces-trimmed": "^2.3.4 || ^1.2 || ^0",
"combination-or-version-range-caret-unsorted/02-without-spaces-untrimmed": "^2.3.4 || ^1.2 || ^0",
"combination-or-version-range-caret-unsorted/03-with-single-space-trimmed": "^2.3.4 || ^1.2 || ^0",
"combination-or-version-range-caret-unsorted/04-with-single-space-untrimmed": "^2.3.4 || ^1.2 || ^0",
"combination-or-version-range-caret-unsorted/05-with-double-space-trimmed": "^2.3.4 || ^1.2 || ^0",
"combination-or-version-range-caret-unsorted/06-with-double-space-untrimmed": "^2.3.4 || ^1.2 || ^0"
"combination-or-version-range-caret-unsorted/01-without-spaces-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-unsorted/02-without-spaces-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-unsorted/03-with-single-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-unsorted/04-with-single-space-untrimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-unsorted/05-with-double-space-trimmed": "^0 || ^1.2 || ^2.3.4",
"combination-or-version-range-caret-unsorted/06-with-double-space-untrimmed": "^0 || ^1.2 || ^2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"homepage": "https://getcomposer.org/doc/articles/versions.md#version-range",
"value-contains-packages-and-version-constraints": {
"combination-or-version-range-tilde-unsorted/01-without-spaces-trimmed": "~2.3.4 || ~1.2 || ~0",
"combination-or-version-range-tilde-unsorted/02-without-spaces-untrimmed": "~2.3.4 || ~1.2 || ~0",
"combination-or-version-range-tilde-unsorted/03-with-single-space-trimmed": "~2.3.4 || ~1.2 || ~0",
"combination-or-version-range-tilde-unsorted/04-with-single-space-untrimmed": "~2.3.4 || ~1.2 || ~0",
"combination-or-version-range-tilde-unsorted/05-with-double-space-trimmed": "~2.3.4 || ~1.2 || ~0",
"combination-or-version-range-tilde-unsorted/06-with-double-space-untrimmed": "~2.3.4 || ~1.2 || ~0"
"combination-or-version-range-tilde-unsorted/01-without-spaces-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-unsorted/02-without-spaces-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-unsorted/03-with-single-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-unsorted/04-with-single-space-untrimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-unsorted/05-with-double-space-trimmed": "~0 || ~1.2 || ~2.3.4",
"combination-or-version-range-tilde-unsorted/06-with-double-space-untrimmed": "~0 || ~1.2 || ~2.3.4"
}
}