Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune empty sections in composer.json #1053

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
$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 @@

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": "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": {}
}
{}
Loading