From 2f2c6061e8bb0a51c2e373932bbfdeb4efff38cd Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sun, 31 Dec 2023 18:42:16 +0000 Subject: [PATCH] Prune empty sections in composer.json --- README.md | 13 ++++++++ src/SchemaNormalizer.php | 30 ++++++++++++++++--- .../Composer/ComposerJsonNormalizer.php | 1 + .../Bin/IsArray/HasEntries/No/normalized.json | 3 +- .../Config/HasEntries/No/normalized.json | 3 +- .../IsObject/HasEntries/No/normalized.json | 5 +--- .../IsObject/HasEntries/No/normalized.json | 5 +--- .../Yes/IsSortedByKey/No/normalized.json | 1 - .../Yes/IsSortedByKey/Yes/normalized.json | 1 - .../Extra/HasEntries/No/normalized.json | 3 +- .../HasEntries/No/normalized.json | 3 +- .../RequireAndRequireDev/normalized.json | 1 - .../Scripts/HasEntries/No/normalized.json | 3 +- .../Suggest/HasEntries/No/normalized.json | 3 +- .../Yes/IsSortedByKey/Yes/normalized.json | 1 - .../HasEntries/No/normalized.json | 4 +-- 16 files changed, 49 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 72244f1c..5a7f0303 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/SchemaNormalizer.php b/src/SchemaNormalizer.php index 678d44df..1a1644d3 100644 --- a/src/SchemaNormalizer.php +++ b/src/SchemaNormalizer.php @@ -30,10 +30,11 @@ final class SchemaNormalizer implements Normalizer private string $schemaUri; public function __construct( - string $schemaUri, - SchemaStorage $schemaStorage, - SchemaValidator\SchemaValidator $schemaValidator, - Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted + private readonly string $schemaUri, + private readonly SchemaStorage $schemaStorage, + private readonly SchemaValidator\SchemaValidator $schemaValidator, + private readonly Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted, + private readonly bool $pruneEmpty = false, ) { $this->schemaUri = $schemaUri; $this->schemaStorage = $schemaStorage; @@ -223,6 +224,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}); } } @@ -327,4 +336,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; + } + + return true; + } } diff --git a/src/Vendor/Composer/ComposerJsonNormalizer.php b/src/Vendor/Composer/ComposerJsonNormalizer.php index 91b60f76..c7eeb1b5 100644 --- a/src/Vendor/Composer/ComposerJsonNormalizer.php +++ b/src/Vendor/Composer/ComposerJsonNormalizer.php @@ -89,6 +89,7 @@ public function __construct(string $schemaUri) */ Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/scripts/auto-scripts')), ), + true, ), new BinNormalizer(), new ConfigHashNormalizer(), diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Bin/IsArray/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Bin/IsArray/HasEntries/No/normalized.json index 95a2636f..5ca981d1 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Bin/IsArray/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Bin/IsArray/HasEntries/No/normalized.json @@ -13,6 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/04-schema.md#bin", - "bin": [] + "homepage": "https://getcomposer.org/doc/04-schema.md#bin" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/No/normalized.json index 3dcf5ebc..76d18379 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/No/normalized.json @@ -13,6 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/06-config.md", - "config": {} + "homepage": "https://getcomposer.org/doc/06-config.md" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/AllowPlugins/IsObject/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/AllowPlugins/IsObject/HasEntries/No/normalized.json index 6b102425..303a77c1 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/AllowPlugins/IsObject/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/AllowPlugins/IsObject/HasEntries/No/normalized.json @@ -13,8 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/06-config.md#allow-plugins", - "config": { - "allow-plugins": {} - } + "homepage": "https://getcomposer.org/doc/06-config.md#allow-plugins" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/PreferredInstall/IsObject/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/PreferredInstall/IsObject/HasEntries/No/normalized.json index 4f2fdcdf..5e5ee801 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/PreferredInstall/IsObject/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/HasProperty/PreferredInstall/IsObject/HasEntries/No/normalized.json @@ -13,8 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/06-config.md#preferred-install", - "config": { - "preferred-install": {} - } + "homepage": "https://getcomposer.org/doc/06-config.md#preferred-install" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/No/normalized.json index 9214fb01..844f7f18 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/No/normalized.json @@ -15,7 +15,6 @@ ], "homepage": "https://getcomposer.org/doc/06-config.md", "config": { - "allow-plugins": {}, "platform": { "php": "8.0.25" }, diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/Yes/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/Yes/normalized.json index 9214fb01..844f7f18 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/Yes/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Config/HasEntries/Yes/IsSortedByKey/Yes/normalized.json @@ -15,7 +15,6 @@ ], "homepage": "https://getcomposer.org/doc/06-config.md", "config": { - "allow-plugins": {}, "platform": { "php": "8.0.25" }, diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Extra/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Extra/HasEntries/No/normalized.json index 100d082b..dc1ea4b1 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Extra/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Extra/HasEntries/No/normalized.json @@ -13,6 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/04-schema.md#extra", - "extra": {} + "homepage": "https://getcomposer.org/doc/04-schema.md#extra" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/No/normalized.json index e0bf8e59..0b2e7f22 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Repositories/HasEntries/No/normalized.json @@ -13,6 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/04-schema.md#repositories", - "repositories": [] + "homepage": "https://getcomposer.org/doc/04-schema.md#repositories" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json index 602cd33e..6f72b1a7 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json @@ -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", diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Scripts/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Scripts/HasEntries/No/normalized.json index 868436c5..dd69106e 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Scripts/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Scripts/HasEntries/No/normalized.json @@ -13,6 +13,5 @@ "email": "am@localheinz.com" } ], - "homepage": "https://getcomposer.org/doc/04-schema.md#scripts", - "scripts": {} + "homepage": "https://getcomposer.org/doc/04-schema.md#scripts" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/No/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/No/normalized.json index d3c84b1d..15a62d7a 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/No/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/No/normalized.json @@ -1,4 +1,3 @@ { - "homepage": "https://getcomposer.org/doc/04-schema.md#suggest", - "suggest": {} + "homepage": "https://getcomposer.org/doc/04-schema.md#suggest" } diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json index 40459a03..ced72310 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json @@ -1,6 +1,5 @@ { "homepage": "https://getcomposer.org/doc/04-schema.md#suggest", - "require": {}, "suggest": { "php": "Nothing works without it", "hhvm": "Okay", diff --git a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/No/normalized.json b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/No/normalized.json index 8f8beba8..0967ef42 100644 --- a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/No/normalized.json +++ b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/No/normalized.json @@ -1,3 +1 @@ -{ - "value-contains-packages-and-version-constraints": {} -} +{}