From 435d8d6a6342382d979aaf830a05ae67b62413f2 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Thu, 8 Aug 2024 17:51:49 +0300 Subject: [PATCH] Remove array helper `intersectKeyRecursive` --- system/Helpers/Array/ArrayHelper.php | 23 --- .../ArrayHelperIntersectKeyRecursiveTest.php | 186 ------------------ 2 files changed, 209 deletions(-) delete mode 100644 tests/system/Helpers/Array/ArrayHelperIntersectKeyRecursiveTest.php diff --git a/system/Helpers/Array/ArrayHelper.php b/system/Helpers/Array/ArrayHelper.php index 76d97358d114..f3eff03ba7a2 100644 --- a/system/Helpers/Array/ArrayHelper.php +++ b/system/Helpers/Array/ArrayHelper.php @@ -315,27 +315,4 @@ public static function sortValuesByNatural(array &$array, $sortByIndex = null): return strnatcmp((string) $currentValue, (string) $nextValue); }); } - - /** - * Returns a new array from $target with the keys that are in $original - * - * @param array|string|null> $target - * @param array|string|null> $original - * - * @return array|string|null> - */ - public static function intersectKeyRecursive(array $target, array $original): array - { - $result = []; - - foreach ($target as $key => $value) { - if (is_array($value) && isset($original[$key]) && is_array($original[$key])) { - $result[$key] = self::intersectKeyRecursive($value, $original[$key]); - } elseif (array_key_exists($key, $original)) { - $result[$key] = $value; - } - } - - return $result; - } } diff --git a/tests/system/Helpers/Array/ArrayHelperIntersectKeyRecursiveTest.php b/tests/system/Helpers/Array/ArrayHelperIntersectKeyRecursiveTest.php deleted file mode 100644 index c5c2adfc5e21..000000000000 --- a/tests/system/Helpers/Array/ArrayHelperIntersectKeyRecursiveTest.php +++ /dev/null @@ -1,186 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace CodeIgniter\Helpers\Array; - -use CodeIgniter\Test\CIUnitTestCase; -use PHPUnit\Framework\Attributes\Group; - -/** - * @internal - */ -#[Group('Others')] -final class ArrayHelperIntersectKeyRecursiveTest extends CIUnitTestCase -{ - /** - * @var array|string|null> - */ - private array $targetArray; - - protected function setUp(): void - { - parent::setUp(); - - $this->targetArray = [ - 'a' => [ - 'b' => [ - 'c' => [ - 'd' => 'value1', - ], - ], - 'e' => 'value2', - 'f' => [ - 'g' => 'value3', - 'h' => 'value4', - 'i' => [ - 'j' => 'value5', - ], - ], - 'k' => null, - 'l' => [], - 'm' => '', - ], - ]; - } - - public function testShuffleCopy(): void - { - $original = [ - 'a' => [ - 'l' => [], - 'k' => null, - 'e' => 'value2', - 'f' => [ - 'i' => [ - 'j' => 'value5', - ], - 'h' => 'value4', - 'g' => 'value3', - ], - 'm' => '', - 'b' => [ - 'c' => [ - 'd' => 'value1', - ], - ], - ], - ]; - - $this->assertSame($original, ArrayHelper::intersectKeyRecursive($original, $this->targetArray)); - } - - public function testCopyWithAnotherValues(): void - { - $original = [ - 'a' => [ - 'b' => [ - 'c' => [ - 'd' => 'value1_1', - ], - ], - 'e' => 'value2_2', - 'f' => [ - 'g' => 'value3_3', - 'h' => 'value4_4', - 'i' => [ - 'j' => 'value5_5', - ], - ], - 'k' => [], - 'l' => null, - 'm' => 'value6_6', - ], - ]; - - $this->assertSame($original, ArrayHelper::intersectKeyRecursive($original, $this->targetArray)); - } - - public function testEmptyCompare(): void - { - $this->assertSame([], ArrayHelper::intersectKeyRecursive($this->targetArray, [])); - } - - public function testEmptyOriginal(): void - { - $this->assertSame([], ArrayHelper::intersectKeyRecursive([], $this->targetArray)); - } - - public function testCompletelyDifferent(): void - { - $original = [ - 'new_a' => [ - 'new_b' => [ - 'new_c' => [ - 'new_d' => 'value1_1', - ], - ], - 'new_e' => 'value2_2', - 'new_f' => [ - 'new_g' => 'value3_3', - 'new_h' => 'value4_4', - 'new_i' => [ - 'new_j' => 'value5_5', - ], - ], - 'new_k' => [], - 'new_l' => null, - 'new_m' => '', - ], - ]; - - $this->assertSame([], ArrayHelper::intersectKeyRecursive($original, $this->targetArray)); - } - - public function testRecursiveDiffPartlyDifferent(): void - { - $original = [ - 'a' => [ - 'b' => [ - 'new_c' => [ - 'd' => 'value1', - ], - ], - 'e' => 'value2', - 'f' => [ - 'g' => 'value3', - 'new_h' => 'value4', - 'i' => [ - 'new_j' => 'value5', - ], - ], - 'k' => null, - 'new_l' => [], - 'm' => [ - 'new_n' => '', - ], - ], - ]; - - $intersect = [ - 'a' => [ - 'b' => [], - 'e' => 'value2', - 'f' => [ - 'g' => 'value3', - 'i' => [], - ], - 'k' => null, - 'm' => [ - 'new_n' => '', - ], - ], - ]; - - $this->assertSame($intersect, ArrayHelper::intersectKeyRecursive($original, $this->targetArray)); - } -}