Browse Source

Fix some more inconsistencies in json manipulation

Jordi Boggiano 11 years ago
parent
commit
4392be4d29

+ 5 - 0
src/Composer/Json/JsonManipulator.php

@@ -204,6 +204,11 @@ class JsonManipulator
             list($name, $subName) = explode('.', $name, 2);
         }
 
+        // no node to remove
+        if (!isset($decoded[$mainNode][$name]) || ($subName && !isset($decoded[$mainNode][$name][$subName]))) {
+            return true;
+        }
+
         // try and find a match for the subkey
         if ($this->pregMatch('{"'.preg_quote($name).'"\s*:}i', $children)) {
             // find best match for the value of "name"

+ 44 - 0
tests/Composer/Test/Json/JsonManipulatorTest.php

@@ -368,6 +368,50 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase
         }
     }
 }
+'
+            ),
+            'works on child having unmatched name' => array(
+                '{
+    "repositories": {
+        "baz": {
+            "foo": "bar",
+            "bar": "baz"
+        }
+    }
+}',
+                'bar',
+                true,
+                '{
+    "repositories": {
+        "baz": {
+            "foo": "bar",
+            "bar": "baz"
+        }
+    }
+}
+'
+            ),
+            'works on child having duplicate name' => array(
+                '{
+    "repositories": {
+        "foo": {
+            "baz": "qux"
+        },
+        "baz": {
+            "foo": "bar",
+            "bar": "baz"
+        }
+    }
+}',
+                'baz',
+                true,
+                '{
+    "repositories": {
+        "foo": {
+            "baz": "qux"
+        }
+    }
+}
 '
             ),
             'works on empty repos' => array(