Make sure empty objects are not left behind when removing requires/.. fixes #9462
parent
dfca939f3b
commit
e5a009ed80
|
@ -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;
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"conflict": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"provide": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"replace": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"require-dev": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"name": "my-vend/my-app",
|
||||
"license": "MIT",
|
||||
"suggest": {
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue