[TASK] Specific exception in ArrayUtility::removeByPath() 24/55424/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Mon, 22 Jan 2018 13:36:42 +0000 (14:36 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Tue, 23 Jan 2018 17:05:02 +0000 (18:05 +0100)
Similar to getValueByPath(), removeByPath() now throws
a specific exception if the path to remove does not
exist in given array to avoid catching global \RuntimeException'
in consuming code.
The same "MissingArrayPathException" is used here since
both cases have identical semantic.

Change-Id: I69447c47d24e28110cf03929ea5ed28cf5e43024
Resolves: #83636
Related: #83615
Releases: master
Reviewed-on: https://review.typo3.org/55424
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Reiner Teubner <reiner.teubner@me.com>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/core/Classes/Utility/ArrayUtility.php
typo3/sysext/core/Classes/Utility/Exception/MissingArrayPathException.php
typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php

index f332e4f..45c65a0 100644 (file)
@@ -329,7 +329,7 @@ class ArrayUtility
                 throw new \RuntimeException('Invalid path segment specified', 1371757720);
             }
             if (!array_key_exists($segment, $pointer)) {
-                throw new \RuntimeException('Segment ' . $segment . ' of path ' . $path . ' does not exist in array', 1371758436);
+                throw new MissingArrayPathException('Segment ' . $segment . ' of path ' . $path . ' does not exist in array', 1371758436);
             }
             if ($currentDepth === $pathDepth) {
                 unset($pointer[$segment]);
index 04e21d0..c49c4b8 100644 (file)
@@ -16,7 +16,8 @@ namespace TYPO3\CMS\Core\Utility\Exception;
  */
 
 /**
- * Exception thrown if ArrayUtility::getValueByPath() does not find target path in array
+ * Exception thrown if ArrayUtility::getValueByPath() and
+ * ArrayUtility::removeByPath() don't find the target path in given array.
  *
  * Note this extends from \RuntimeException to be backwards compatible with the
  * formerly thrown \RuntimeException in the method.
index 2f3594f..eb36c19 100644 (file)
@@ -792,6 +792,23 @@ class ArrayUtilityTest extends UnitTestCase
     /**
      * @test
      */
+    public function removeByPathThrowsSpecificExceptionIfPathDoesNotExistInArray()
+    {
+        $inputArray = [
+            'foo' => [
+                'bar' => 42,
+            ]
+        ];
+
+        $this->expectException(MissingArrayPathException::class);
+        $this->expectExceptionCode(1371758436);
+
+        ArrayUtility::removeByPath($inputArray, 'foo/baz');
+    }
+
+    /**
+     * @test
+     */
     public function removeByPathAcceptsGivenDelimiter()
     {
         $inputArray = [