Skip to content

Commit

Permalink
Prune empty sections in composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden authored and localheinz committed Feb 17, 2024
1 parent b6c84b1 commit 2a5e4d2
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 28 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,19 @@ sections, the `Vendor\Composer\VersionConstraintNormalizer` will ensure that
}
```

- empty sections (which are defined as optional in the schema) are automatically removed

```diff
{
"require": {
"foo/bar": "^2.3.4"
}
- "config": {
- "preferred-install": {}
- }
}
```

## Changelog

The maintainers of this project record notable changes to this project in a [changelog](CHANGELOG.md).
Expand Down
26 changes: 25 additions & 1 deletion src/SchemaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ final class SchemaNormalizer implements Normalizer
private SchemaValidator\SchemaValidator $schemaValidator;
private SchemaStorage $schemaStorage;
private string $schemaUri;
private bool $pruneEmpty;

public function __construct(
string $schemaUri,
SchemaStorage $schemaStorage,
SchemaValidator\SchemaValidator $schemaValidator,
Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted
Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted,
bool $pruneEmpty = false
) {
$this->schemaUri = $schemaUri;
$this->schemaStorage = $schemaStorage;
$this->schemaValidator = $schemaValidator;
$this->specificationForPointerToDataThatShouldNotBeSorted = $specificationForPointerToDataThatShouldNotBeSorted;
$this->pruneEmpty = $pruneEmpty;
}

public function normalize(Json $json): Json
Expand Down Expand Up @@ -223,6 +226,14 @@ private function normalizeObject(
$pointerToData->append(Pointer\ReferenceToken::fromString($name)),
);

if (
$this->pruneEmpty
&& [] === (array) $normalized->{$name}
&& self::isKeyOptionalInSchema($schema, $name)
) {
unset($normalized->{$name});
}

unset($data->{$name});
}
}
Expand Down Expand Up @@ -327,4 +338,17 @@ private function resolveSchema(

return $schema;
}

private static function isKeyOptionalInSchema(object $schema, string $name): bool
{
if (
\property_exists($schema, 'required')
&& \is_array($schema->required)
&& \in_array($name, $schema->required, true)
) {
return false;

Check warning on line 349 in src/SchemaNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/SchemaNormalizer.php#L349

Added line #L349 was not covered by tests
}

return true;
}
}
1 change: 1 addition & 0 deletions src/Vendor/Composer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function __construct(string $schemaUri)
*/
Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/scripts/auto-scripts')),
),
true,
),
new BinNormalizer(),
new ConfigHashNormalizer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/04-schema.md#bin",
"bin": []
"homepage": "https://getcomposer.org/doc/04-schema.md#bin"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/06-config.md",
"config": {}
"homepage": "https://getcomposer.org/doc/06-config.md"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/06-config.md#allow-plugins",
"config": {
"allow-plugins": {}
}
"homepage": "https://getcomposer.org/doc/06-config.md#allow-plugins"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/06-config.md#preferred-install",
"config": {
"preferred-install": {}
}
"homepage": "https://getcomposer.org/doc/06-config.md#preferred-install"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
],
"homepage": "https://getcomposer.org/doc/06-config.md",
"config": {
"allow-plugins": {},
"platform": {
"php": "8.0.25"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
],
"homepage": "https://getcomposer.org/doc/06-config.md",
"config": {
"allow-plugins": {},
"platform": {
"php": "8.0.25"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/04-schema.md#extra",
"extra": {}
"homepage": "https://getcomposer.org/doc/04-schema.md#extra"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/04-schema.md#repositories",
"repositories": []
"homepage": "https://getcomposer.org/doc/04-schema.md#repositories"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"description": "This test exists to cover certain mutants from infection/infection",
"require": {},
"require-dev": {
"ergebnis/overlapping-version-constraints-1": "^1.0 || 3.4.5",
"ergebnis/overlapping-version-constraints-2": "^1.0 || ^2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"email": "[email protected]"
}
],
"homepage": "https://getcomposer.org/doc/04-schema.md#scripts",
"scripts": {}
"homepage": "https://getcomposer.org/doc/04-schema.md#scripts"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"homepage": "https://getcomposer.org/doc/04-schema.md#suggest",
"suggest": {}
"homepage": "https://getcomposer.org/doc/04-schema.md#suggest"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"homepage": "https://getcomposer.org/doc/04-schema.md#suggest",
"require": {},
"suggest": {
"php": "Nothing works without it",
"hhvm": "Okay",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"value-contains-packages-and-version-constraints": {}
}
{}

0 comments on commit 2a5e4d2

Please sign in to comment.