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

Fix: Do not sort preferred-install hash #425

Merged
merged 2 commits into from
Dec 30, 2020
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
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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 [`1.0.0...main`][1.0.0...main].
For a full diff see [`1.0.1...main`][1.0.1...main].

## [`1.0.1`][1.0.1]

For a full diff see [`1.0.0...1.0.1`][1.0.0...1.0.1].

### Fixed

* Adjusted `Vendor\Composer\ConfigHashNormalizer` to ignore the `preferred-install` hash ([#425]), by [@localheinz]

## [`1.0.0`][1.0.0]

Expand Down Expand Up @@ -293,6 +301,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.14.0]: https://github.com/ergebnis/json-normalizer/releases/tag/0.14.0
[0.14.1]: https://github.com/ergebnis/json-normalizer/releases/tag/0.14.1
[1.0.0]: https://github.com/ergebnis/json-normalizer/releases/tag/1.0.0
[1.0.1]: https://github.com/ergebnis/json-normalizer/releases/tag/1.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 All @@ -314,7 +323,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.13.1...0.14.0]: https://github.com/ergebnis/json-normalizer/compare/0.13.1...0.14.0
[0.14.0...0.14.1]: https://github.com/ergebnis/json-normalizer/compare/0.14.0...0.14.1
[0.14.1...1.0.0]: https://github.com/ergebnis/json-normalizer/compare/0.14.1...1.0.0
[1.0.0...main]: https://github.com/ergebnis/json-normalizer/compare/1.0.0...main
[1.0.0...1.0.1]: https://github.com/ergebnis/json-normalizer/compare/1.0.0...1.0.0
[1.0.1...main]: https://github.com/ergebnis/json-normalizer/compare/1.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 @@ -378,6 +388,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#384]: https://github.com/ergebnis/json-normalizer/pull/384
[#423]: https://github.com/ergebnis/json-normalizer/pull/423
[#424]: https://github.com/ergebnis/json-normalizer/pull/424
[#425]: https://github.com/ergebnis/json-normalizer/pull/425

[@BackEndTea]: https://github.com/BackEndTea
[@ergebnis]: https://github.com/ergebnis
Expand Down
36 changes: 31 additions & 5 deletions src/Vendor/Composer/ConfigHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ final class ConfigHashNormalizer implements NormalizerInterface
'scripts-descriptions',
];

/**
* @phpstan-var list<string>
* @psalm-var list<string>
*
* @var array<int, string>
*/
private static $propertiesThatShouldNotBeSorted = [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use a const for this?

'preferred-install',
];

public function normalize(Json $json): Json
{
$decoded = $json->decoded();
Expand All @@ -48,7 +58,10 @@ public function normalize(Json $json): Json
}

foreach ($objectProperties as $name => $value) {
$decoded->{$name} = self::sortByKey($value);
$decoded->{$name} = self::sortByKey(
$name,
$value
);
}

/** @var string $encoded */
Expand All @@ -62,12 +75,17 @@ public function normalize(Json $json): Json
*
* @return null|array|bool|false|\stdClass|string
*/
private static function sortByKey($value)
private static function sortByKey(string $name, $value)
{
if (\in_array($name, self::$propertiesThatShouldNotBeSorted, true)) {
return $value;
}

if (!\is_object($value)) {
return $value;
}

/** @var array<string, mixed> $sorted */
$sorted = (array) $value;

if ([] === $sorted) {
Expand All @@ -76,8 +94,16 @@ private static function sortByKey($value)

\ksort($sorted);

return \array_map(static function ($value) {
return self::sortByKey($value);
}, $sorted);
$names = \array_keys($sorted);

return \array_combine(
$names,
\array_map(static function ($value, string $name) {
return self::sortByKey(
$name,
$value
);
}, $sorted, $names)
);
}
}
43 changes: 43 additions & 0 deletions test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ public function testNormalizeSortsConfigHashRecursivelyIfPropertyExists(string $
self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded());
}

/**
* @see https://github.com/ergebnis/composer-normalize/issues/644
* @see https://getcomposer.org/doc/06-config.md#preferred-install
*/
public function testNormalizeDoesNotSortPreferredInstall(): void
{
$json = Json::fromEncoded(
<<<'JSON'
{
"config": {
"sort-packages": true,
"preferred-install": {
"foo/*": "source",
"bar/*": "source",
"*": "dist"
}
}
}
JSON
);

$expected = Json::fromEncoded(
<<<'JSON'
{
"config": {
"preferred-install": {
"foo/*": "source",
"bar/*": "source",
"*": "dist"
},
"sort-packages": true
}
}
JSON
);

$normalizer = new ConfigHashNormalizer();

$normalized = $normalizer->normalize($json);

self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded());
}

/**
* @return \Generator<array<string>>
*/
Expand Down