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 2f2c606
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 31 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
30 changes: 26 additions & 4 deletions src/SchemaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ final class SchemaNormalizer implements Normalizer
private string $schemaUri;

public function __construct(

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, lowest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, locked)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.1, highest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, lowest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, locked)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.2, highest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, lowest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, locked)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri

Check failure on line 32 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Tests (8.3, highest)

Cannot redeclare Ergebnis\Json\Normalizer\SchemaNormalizer::$schemaUri
string $schemaUri,
SchemaStorage $schemaStorage,
SchemaValidator\SchemaValidator $schemaValidator,
Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted
private readonly string $schemaUri,

Check failure on line 33 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

InvalidDocblock

src/SchemaNormalizer.php:33:9: InvalidDocblock: Param1 of Ergebnis\Json\Normalizer\SchemaNormalizer::__construct has invalid syntax (see https://psalm.dev/008)

Check failure on line 33 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

ParseError

src/SchemaNormalizer.php:33:26: ParseError: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 33 (see https://psalm.dev/173)
private readonly SchemaStorage $schemaStorage,
private readonly SchemaValidator\SchemaValidator $schemaValidator,
private readonly Pointer\Specification $specificationForPointerToDataThatShouldNotBeSorted,
private readonly bool $pruneEmpty = false,
) {
$this->schemaUri = $schemaUri;

Check failure on line 39 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

MixedAssignment

src/SchemaNormalizer.php:39:9: MixedAssignment: Unable to determine the type that $this->schemaUri is being assigned to (see https://psalm.dev/032)

Check failure on line 39 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

UndefinedVariable

src/SchemaNormalizer.php:39:28: UndefinedVariable: Cannot find referenced variable $schemaUri (see https://psalm.dev/024)
$this->schemaStorage = $schemaStorage;

Check failure on line 40 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

MixedAssignment

src/SchemaNormalizer.php:40:9: MixedAssignment: Unable to determine the type that $this->schemaStorage is being assigned to (see https://psalm.dev/032)

Check failure on line 40 in src/SchemaNormalizer.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (7.4, locked)

UndefinedVariable

src/SchemaNormalizer.php:40:32: UndefinedVariable: Cannot find referenced variable $schemaStorage (see https://psalm.dev/024)
Expand Down Expand Up @@ -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});
}
}
Expand Down Expand Up @@ -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;
}
}
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": "am@localheinz.com"
}
],
"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": "am@localheinz.com"
}
],
"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": "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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
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": "am@localheinz.com"
}
],
"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": "am@localheinz.com"
}
],
"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": "am@localheinz.com"
}
],
"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 2f2c606

Please sign in to comment.