Skip to content

Commit fe3aea2

Browse files
authored
Merge pull request #425 from ergebnis/fix/preferred-install
Fix: Do not sort preferred-install hash
2 parents f318c64 + e5e4efb commit fe3aea2

3 files changed

Lines changed: 87 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9-
For a full diff see [`1.0.0...main`][1.0.0...main].
9+
For a full diff see [`1.0.1...main`][1.0.1...main].
10+
11+
## [`1.0.1`][1.0.1]
12+
13+
For a full diff see [`1.0.0...1.0.1`][1.0.0...1.0.1].
14+
15+
### Fixed
16+
17+
* Adjusted `Vendor\Composer\ConfigHashNormalizer` to ignore the `preferred-install` hash ([#425]), by [@localheinz]
1018

1119
## [`1.0.0`][1.0.0]
1220

@@ -293,6 +301,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
293301
[0.14.0]: https://github.com/ergebnis/json-normalizer/releases/tag/0.14.0
294302
[0.14.1]: https://github.com/ergebnis/json-normalizer/releases/tag/0.14.1
295303
[1.0.0]: https://github.com/ergebnis/json-normalizer/releases/tag/1.0.0
304+
[1.0.1]: https://github.com/ergebnis/json-normalizer/releases/tag/1.0.1
296305

297306
[5d8b3e2...0.1.0]: https://github.com/ergebnis/json-normalizer/compare/5d8b3e2...0.1.0
298307
[0.1.0...0.2.0]: https://github.com/ergebnis/json-normalizer/compare/0.1.0...0.2.0
@@ -314,7 +323,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
314323
[0.13.1...0.14.0]: https://github.com/ergebnis/json-normalizer/compare/0.13.1...0.14.0
315324
[0.14.0...0.14.1]: https://github.com/ergebnis/json-normalizer/compare/0.14.0...0.14.1
316325
[0.14.1...1.0.0]: https://github.com/ergebnis/json-normalizer/compare/0.14.1...1.0.0
317-
[1.0.0...main]: https://github.com/ergebnis/json-normalizer/compare/1.0.0...main
326+
[1.0.0...1.0.1]: https://github.com/ergebnis/json-normalizer/compare/1.0.0...1.0.0
327+
[1.0.1...main]: https://github.com/ergebnis/json-normalizer/compare/1.0.1...main
318328

319329
[#1]: https://github.com/ergebnis/json-normalizer/pull/1
320330
[#2]: https://github.com/ergebnis/json-normalizer/pull/2
@@ -378,6 +388,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
378388
[#384]: https://github.com/ergebnis/json-normalizer/pull/384
379389
[#423]: https://github.com/ergebnis/json-normalizer/pull/423
380390
[#424]: https://github.com/ergebnis/json-normalizer/pull/424
391+
[#425]: https://github.com/ergebnis/json-normalizer/pull/425
381392

382393
[@BackEndTea]: https://github.com/BackEndTea
383394
[@ergebnis]: https://github.com/ergebnis

src/Vendor/Composer/ConfigHashNormalizer.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ final class ConfigHashNormalizer implements NormalizerInterface
3030
'scripts-descriptions',
3131
];
3232

33+
/**
34+
* @phpstan-var list<string>
35+
* @psalm-var list<string>
36+
*
37+
* @var array<int, string>
38+
*/
39+
private static $propertiesThatShouldNotBeSorted = [
40+
'preferred-install',
41+
];
42+
3343
public function normalize(Json $json): Json
3444
{
3545
$decoded = $json->decoded();
@@ -48,7 +58,10 @@ public function normalize(Json $json): Json
4858
}
4959

5060
foreach ($objectProperties as $name => $value) {
51-
$decoded->{$name} = self::sortByKey($value);
61+
$decoded->{$name} = self::sortByKey(
62+
$name,
63+
$value
64+
);
5265
}
5366

5467
/** @var string $encoded */
@@ -62,12 +75,17 @@ public function normalize(Json $json): Json
6275
*
6376
* @return null|array|bool|false|\stdClass|string
6477
*/
65-
private static function sortByKey($value)
78+
private static function sortByKey(string $name, $value)
6679
{
80+
if (\in_array($name, self::$propertiesThatShouldNotBeSorted, true)) {
81+
return $value;
82+
}
83+
6784
if (!\is_object($value)) {
6885
return $value;
6986
}
7087

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

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

7795
\ksort($sorted);
7896

79-
return \array_map(static function ($value) {
80-
return self::sortByKey($value);
81-
}, $sorted);
97+
$names = \array_keys($sorted);
98+
99+
return \array_combine(
100+
$names,
101+
\array_map(static function ($value, string $name) {
102+
return self::sortByKey(
103+
$name,
104+
$value
105+
);
106+
}, $sorted, $names)
107+
);
82108
}
83109
}

test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,49 @@ public function testNormalizeSortsConfigHashRecursivelyIfPropertyExists(string $
196196
self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded());
197197
}
198198

199+
/**
200+
* @see https://github.com/ergebnis/composer-normalize/issues/644
201+
* @see https://getcomposer.org/doc/06-config.md#preferred-install
202+
*/
203+
public function testNormalizeDoesNotSortPreferredInstall(): void
204+
{
205+
$json = Json::fromEncoded(
206+
<<<'JSON'
207+
{
208+
"config": {
209+
"sort-packages": true,
210+
"preferred-install": {
211+
"foo/*": "source",
212+
"bar/*": "source",
213+
"*": "dist"
214+
}
215+
}
216+
}
217+
JSON
218+
);
219+
220+
$expected = Json::fromEncoded(
221+
<<<'JSON'
222+
{
223+
"config": {
224+
"preferred-install": {
225+
"foo/*": "source",
226+
"bar/*": "source",
227+
"*": "dist"
228+
},
229+
"sort-packages": true
230+
}
231+
}
232+
JSON
233+
);
234+
235+
$normalizer = new ConfigHashNormalizer();
236+
237+
$normalized = $normalizer->normalize($json);
238+
239+
self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded());
240+
}
241+
199242
/**
200243
* @return \Generator<array<string>>
201244
*/

0 commit comments

Comments
 (0)