Skip to content

Commit

Permalink
Merge pull request #723 from fredden/sort-allow-plugins-preferred-ins…
Browse files Browse the repository at this point in the history
…tall

Sort `allow-plugins` and `preferred-install` properly
  • Loading branch information
localheinz committed Aug 10, 2022
2 parents 748cd50 + 1fb20a0 commit 1c3be30
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 53 deletions.
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,
);
}
}

0 comments on commit 1c3be30

Please sign in to comment.