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) {
$composerDefinition[$requireKey][$package] = $version;
unset($composerDefinition[$removeKey][$package]);
if (isset($composerDefinition[$removeKey]) && count($composerDefinition[$removeKey]) === 0) {
unset($composerDefinition[$removeKey]);
}
}
$this->json->write($composerDefinition);
}
@ -341,6 +344,8 @@ EOT
}
}
$manipulator->removeMainKeyIfEmpty($removeKey);
file_put_contents($json->getPath(), $manipulator->getContents());
return true;

View File

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

View File

@ -480,6 +480,21 @@ class JsonManipulator
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)
{
if (is_array($data)) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2437,6 +2437,48 @@ class JsonManipulatorTest extends TestCase
', $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()
{
$manipulator = new JsonManipulator(json_encode(array(