1
0
Fork 0

Make sure empty objects are not left behind when removing requires/.. fixes #9462

pull/9465/head
Jordi Boggiano 2020-11-12 11:09:15 +01:00
parent dfca939f3b
commit e5a009ed80
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
10 changed files with 70 additions and 19 deletions

View File

@ -226,6 +226,9 @@ EOT
foreach ($requirements as $package => $version) { foreach ($requirements as $package => $version) {
$composerDefinition[$requireKey][$package] = $version; $composerDefinition[$requireKey][$package] = $version;
unset($composerDefinition[$removeKey][$package]); unset($composerDefinition[$removeKey][$package]);
if (isset($composerDefinition[$removeKey]) && count($composerDefinition[$removeKey]) === 0) {
unset($composerDefinition[$removeKey]);
}
} }
$this->json->write($composerDefinition); $this->json->write($composerDefinition);
} }
@ -341,6 +344,8 @@ EOT
} }
} }
$manipulator->removeMainKeyIfEmpty($removeKey);
file_put_contents($json->getPath(), $manipulator->getContents()); file_put_contents($json->getPath(), $manipulator->getContents());
return true; return true;

View File

@ -196,7 +196,8 @@ class JsonConfigSource implements ConfigSourceInterface
{ {
$this->manipulateJson('removeSubNode', $type, $name, function (&$config, $type, $name) { $this->manipulateJson('removeSubNode', $type, $name, function (&$config, $type, $name) {
unset($config[$type][$name]); unset($config[$type][$name]);
});
$this->manipulateJson('removeMainKeyIfEmpty', $type, function (&$config, $type) {
if (0 === count($config[$type])) { if (0 === count($config[$type])) {
unset($config[$type]); unset($config[$type]);
} }

View File

@ -480,6 +480,21 @@ class JsonManipulator
return false; return false;
} }
public function removeMainKeyIfEmpty($key)
{
$decoded = JsonFile::parseJson($this->contents);
if (!array_key_exists($key, $decoded)) {
return true;
}
if (is_array($decoded[$key]) && count($decoded[$key]) === 0) {
return $this->removeMainKey($key);
}
return false;
}
public function format($data, $depth = 0) public function format($data, $depth = 0)
{ {
if (is_array($data)) { if (is_array($data)) {

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"conflict": {
}
} }

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"provide": {
}
} }

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"replace": {
}
} }

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"require-dev": {
}
} }

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"require": {
}
} }

View File

@ -1,6 +1,4 @@
{ {
"name": "my-vend/my-app", "name": "my-vend/my-app",
"license": "MIT", "license": "MIT"
"suggest": {
}
} }

View File

@ -2437,6 +2437,48 @@ class JsonManipulatorTest extends TestCase
', $manipulator->getContents()); ', $manipulator->getContents());
} }
public function testRemoveMainKeyIfEmpty()
{
$manipulator = new JsonManipulator('{
"repositories": [
],
"require": {
"package/a": "*",
"package/b": "*",
"package/c": "*"
},
"foo": "bar",
"require-dev": {
}
}');
$this->assertTrue($manipulator->removeMainKeyIfEmpty('repositories'));
$this->assertEquals('{
"require": {
"package/a": "*",
"package/b": "*",
"package/c": "*"
},
"foo": "bar",
"require-dev": {
}
}
', $manipulator->getContents());
$this->assertFalse($manipulator->removeMainKeyIfEmpty('foo'));
$this->assertFalse($manipulator->removeMainKeyIfEmpty('require'));
$this->assertTrue($manipulator->removeMainKeyIfEmpty('require-dev'));
$this->assertEquals('{
"require": {
"package/a": "*",
"package/b": "*",
"package/c": "*"
},
"foo": "bar"
}
', $manipulator->getContents());
}
public function testRemoveMainKeyRemovesKeyWhereValueIsNull() public function testRemoveMainKeyRemovesKeyWhereValueIsNull()
{ {
$manipulator = new JsonManipulator(json_encode(array( $manipulator = new JsonManipulator(json_encode(array(