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 allow-plugins and preferred-install properly #723

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions CHANGELOG.md
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`3.0.0...main`][3.0.0...main].
For a full diff see [`3.0.1...main`][3.0.1...main].

## [`3.0.1`][3.0.1]

For a full diff see [`3.0.0...3.0.1`][3.0.0...3.0.1].

### Fixed

- Adjusted `ConfigHashNormalizer` to sort keys correctly ([#723]), by [@fredded]

### Changed

Expand Down Expand Up @@ -385,6 +393,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[2.1.0]: https://github.com/ergebnis/json-normalizer/releases/tag/2.1.0
[2.2.0]: https://github.com/ergebnis/json-normalizer/releases/tag/2.2.0
[3.0.0]: https://github.com/ergebnis/json-normalizer/releases/tag/3.0.0
[3.0.1]: https://github.com/ergebnis/json-normalizer/releases/tag/3.0.1

[5d8b3e2...0.1.0]: https://github.com/ergebnis/json-normalizer/compare/5d8b3e2...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/json-normalizer/compare/0.1.0...0.2.0
Expand Down Expand Up @@ -413,7 +422,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[2.0.0...2.1.0]: https://github.com/ergebnis/json-normalizer/compare/2.0.0...2.1.0
[2.1.0...2.2.0]: https://github.com/ergebnis/json-normalizer/compare/2.1.0...2.2.0
[2.2.0...3.0.0]: https://github.com/ergebnis/json-normalizer/compare/2.2.0...3.0.0
[3.0.0...main]: https://github.com/ergebnis/json-normalizer/compare/3.0.0...main
[3.0.0...3.0.1]: https://github.com/ergebnis/json-normalizer/compare/3.0.0...3.0.1
[3.0.1...main]: https://github.com/ergebnis/json-normalizer/compare/3.0.1...main

[#1]: https://github.com/ergebnis/json-normalizer/pull/1
[#2]: https://github.com/ergebnis/json-normalizer/pull/2
Expand Down Expand Up @@ -504,8 +514,10 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#673]: https://github.com/ergebnis/json-normalizer/pull/673
[#697]: https://github.com/ergebnis/json-normalizer/pull/697
[#698]: https://github.com/ergebnis/json-normalizer/pull/698
[#723]: https://github.com/ergebnis/json-normalizer/pull/723

[@BackEndTea]: https://github.com/BackEndTea
[@dependabot]: https://github.com/dependabot
[@ergebnis]: https://github.com/ergebnis
[@fredden]: https://github.com/fredden
[@localheinz]: https://github.com/localheinz
38 changes: 24 additions & 14 deletions src/Vendor/Composer/ConfigHashNormalizer.php
Expand Up @@ -24,15 +24,6 @@ final class ConfigHashNormalizer implements Normalizer
'scripts-descriptions',
];

/**
* @see https://getcomposer.org/doc/06-config.md#allow-plugins
* @see https://getcomposer.org/doc/06-config.md#preferred-install
*/
private const PROPERTY_PATHS_THAT_SHOULD_NOT_BE_SORTED = [
'config.allow-plugins',
'config.preferred-install',
];

public function normalize(Json $json): Json
{
$decoded = $json->decoded();
Expand Down Expand Up @@ -72,10 +63,6 @@ private static function sortByKey(
string $propertyPath,
$value
) {
if (\in_array($propertyPath, self::PROPERTY_PATHS_THAT_SHOULD_NOT_BE_SORTED, true)) {
return $value;
}

if (!\is_object($value)) {
return $value;
}
Expand All @@ -87,7 +74,12 @@ private static function sortByKey(
return $value;
}

\ksort($sorted);
\uksort($sorted, static function (string $a, string $b): int {
return \strcmp(
self::normalizeKey($a),
self::normalizeKey($b),
);
});

$names = \array_keys($sorted);

Expand All @@ -105,4 +97,22 @@ private static function sortByKey(
}, $sorted, $names),
);
}

/**
* Replaces characters in keys to ensure the correct order.
*
* - '*' = ASCII 42 (i.e., before all letters, numbers, and dash)
* - '~' = ASCII 126 (i.e., after all letters, numbers, and dash)
*
* @see https://getcomposer.org/doc/06-config.md#allow-plugins
* @see https://getcomposer.org/doc/06-config.md#preferred-install
*/
private static function normalizeKey(string $key): string
{
return \str_replace(
'*',
'~',
$key,
);
}
}