Skip to content

Commit

Permalink
Merge pull request #60 from localheinz/feature/extra
Browse files Browse the repository at this point in the history
Enhancement: Adjust ConfigHashNormalizer to also sort extra section
  • Loading branch information
localheinz committed Apr 24, 2018
2 parents 90d6827 + 89fb7ed commit 5dd2a43
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ the `BinNormalizer` will sort the elements of the `bin` section by value in asce

### `ConfigHashNormalizer`

If `composer.json` contains any configuration in the `config` section,
the `ConfigHashNormalizer` will sort the `config` section by key in ascending order.
If `composer.json` contains any configuration in the

* `config`
* `extra`

sections, the `ConfigHashNormalizer` will sort the content of these sections
by key in ascending order.

:bulb: Find out more about the `config` section at https://getcomposer.org/doc/06-config.md.

Expand Down
29 changes: 22 additions & 7 deletions src/Normalizer/ConfigHashNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@

final class ConfigHashNormalizer implements NormalizerInterface
{
/**
* @var string[]
*/
private static $properties = [
'config',
'extra',
];

public function normalize(string $json): string
{
$decoded = \json_decode($json);
Expand All @@ -28,19 +36,26 @@ public function normalize(string $json): string
));
}

if (!\property_exists($decoded, 'config')) {
$objectProperties = \array_intersect_key(
\get_object_vars($decoded),
\array_flip(self::$properties)
);

if (!\count($objectProperties)) {
return $json;
}

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

if (!\count($config)) {
return $json;
}
if (!\count($config)) {
return $json;
}

\ksort($config);
\ksort($config);

$decoded->config = $config;
$decoded->{$name} = $config;
}

return \json_encode($decoded);
}
Expand Down
43 changes: 35 additions & 8 deletions test/Unit/Normalizer/ConfigHashNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ public function testNormalizeDoesNotModifyOtherProperty(): void
$this->assertSame($json, $normalizer->normalize($json));
}

public function testNormalizeIgnoresEmptyConfigHash(): void
/**
* @dataProvider providerProperty
*
* @param string $property
*/
public function testNormalizeIgnoresEmptyConfigHash(string $property): void
{
$json = <<<'JSON'
$json = <<<JSON
{
"config": {}
"${property}": {}
}
JSON;

Expand All @@ -46,11 +51,16 @@ public function testNormalizeIgnoresEmptyConfigHash(): void
$this->assertSame($json, $normalizer->normalize($json));
}

public function testNormalizeSortsConfigHashIfPropertyExists(): void
/**
* @dataProvider providerProperty
*
* @param string $property
*/
public function testNormalizeSortsConfigHashIfPropertyExists(string $property): void
{
$json = <<<'JSON'
$json = <<<JSON
{
"config": {
"${property}": {
"sort-packages": true,
"preferred-install": "dist"
},
Expand All @@ -61,9 +71,9 @@ public function testNormalizeSortsConfigHashIfPropertyExists(): void
}
JSON;

$normalized = <<<'JSON'
$normalized = <<<JSON
{
"config": {
"${property}": {
"preferred-install": "dist",
"sort-packages": true
},
Expand All @@ -78,4 +88,21 @@ public function testNormalizeSortsConfigHashIfPropertyExists(): void

$this->assertSame(\json_encode(\json_decode($normalized)), $normalizer->normalize($json));
}

public function providerProperty(): \Generator
{
foreach ($this->properties() as $value) {
yield $value => [
$value,
];
}
}

private function properties(): array
{
return [
'config',
'extra',
];
}
}

0 comments on commit 5dd2a43

Please sign in to comment.