Skip to content

Commit

Permalink
Merge pull request #424 from ergebnis/feature/recursive
Browse files Browse the repository at this point in the history
Enhancement: Let ConfigHashNormalizer recursively sort hashes by key
  • Loading branch information
ergebnis-bot authored Dec 29, 2020
2 parents 55e2515 + bc274fe commit f318c64
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
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 [`0.14.1...main`][0.14.1...main].
For a full diff see [`1.0.0...main`][1.0.0...main].

## [`1.0.0`][1.0.0]

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

### Changed

* Adjusted `Vendor\Composer\ConfigHashNormalizer` to recursively sort hashes by key ([#424]), by [@localheinz]

## [`0.14.1`][0.14.1]

Expand Down Expand Up @@ -284,6 +292,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.13.1]: https://github.com/ergebnis/json-normalizer/releases/tag/0.13.1
[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

[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 @@ -304,7 +313,8 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[0.13.0...0.13.1]: https://github.com/ergebnis/json-normalizer/compare/0.13.0...0.13.1
[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...main]: https://github.com/ergebnis/json-normalizer/compare/0.14.1...main
[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]: https://github.com/ergebnis/json-normalizer/pull/1
[#2]: https://github.com/ergebnis/json-normalizer/pull/2
Expand Down Expand Up @@ -367,6 +377,7 @@ For a full diff see [`5d8b3e2...0.1.0`][5d8b3e2...0.1.0].
[#335]: https://github.com/ergebnis/json-normalizer/pull/335
[#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

[@BackEndTea]: https://github.com/BackEndTea
[@ergebnis]: https://github.com/ergebnis
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ When `composer.json` contains any configuration in the
* `extra`
* `scripts-descriptions`

sections, the `Vendor\Composer\ConfigHashNormalizer` will sort the content of these sections by key in ascending order.
sections, the `Vendor\Composer\ConfigHashNormalizer` will sort the content of these sections by key in ascending order. If a value is an object, it will continue to sort its properties by name.

:bulb: Find out more about the `config` section at [Composer: The composer.json schema](https://getcomposer.org/doc/06-config.md).

Expand Down
7 changes: 4 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
</MixedReturnStatement>
</file>
<file src="src/Vendor/Composer/ConfigHashNormalizer.php">
<MixedArgument occurrences="2">
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedAssignment occurrences="1">
<code>$value</code>
</MixedAssignment>
<UnusedVariable occurrences="1">
<code>$value</code>
</UnusedVariable>
</file>
<file src="src/Vendor/Composer/PackageHashNormalizer.php">
<MixedAssignment occurrences="1">
Expand Down
34 changes: 25 additions & 9 deletions src/Vendor/Composer/ConfigHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,36 @@ public function normalize(Json $json): Json
}

foreach ($objectProperties as $name => $value) {
$config = (array) $decoded->{$name};

if (0 === \count($config)) {
continue;
}

\ksort($config);

$decoded->{$name} = $config;
$decoded->{$name} = self::sortByKey($value);
}

/** @var string $encoded */
$encoded = \json_encode($decoded);

return Json::fromEncoded($encoded);
}

/**
* @param null|array|bool|false|\stdClass|string $value
*
* @return null|array|bool|false|\stdClass|string
*/
private static function sortByKey($value)
{
if (!\is_object($value)) {
return $value;
}

$sorted = (array) $value;

if ([] === $sorted) {
return $value;
}

\ksort($sorted);

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

/**
* @dataProvider provideProperty
*/
public function testNormalizeSortsConfigHashRecursivelyIfPropertyExists(string $property): void
{
$json = Json::fromEncoded(
<<<JSON
{
"{$property}": {
"sort-packages": true,
"preferred-install": "dist",
"foo": {
"qux": "quux",
"bar": {
"qux": "quz",
"baz": "qux"
}
}
},
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
);

$expected = Json::fromEncoded(
<<<JSON
{
"{$property}": {
"foo": {
"bar": {
"baz": "qux",
"qux": "quz"
},
"qux": "quux"
},
"preferred-install": "dist",
"sort-packages": true
},
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
);

$normalizer = new ConfigHashNormalizer();

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

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

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

0 comments on commit f318c64

Please sign in to comment.