From 816852ef99843adc3e3e1548c8a261f110f78829 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 08:59:23 +0100 Subject: [PATCH 1/8] Improve Arr::dot performance --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index c14465c6b3fa..4b44c4162218 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -113,7 +113,7 @@ public static function dot($array, $prepend = '') foreach ($array as $key => $value) { if (is_array($value) && ! empty($value)) { - $results = array_merge($results, static::dot($value, $prepend.$key.'.')); + $results += static::dot($value, $prepend.$key.'.'); } else { $results[$prepend.$key] = $value; } From 66aa5fa5835f7d189221781cce067a01a293af0b Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 09:15:11 +0100 Subject: [PATCH 2/8] Add some tests for Arr::dot --- tests/Support/SupportArrTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 2c5f6c224e67..3a63f24b7c60 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -113,6 +113,21 @@ public function testDot() $array = Arr::dot(['name' => 'taylor', 'languages' => ['php' => true]]); $this->assertEquals(['name' => 'taylor', 'languages.php' => true], $array); + + $array = Arr::dot(['user' => ['name' => 'Taylor', 'age' => 25, 'languages' => ['PHP', 'C#']]]); + $this->assertEquals([ + 'user.name' => 'Taylor', + 'user.age' => 25, + 'user.languages.0' =>'PHP', + 'user.languages.1' => 'C#' + ], $array); + + $array = Arr::dot(['foo', 'foo' => ['bar' => 'baz', 'baz' => ['a' => 'b']]]); + $this->assertEquals([ + 'foo', + 'foo.bar' => 'baz', + 'foo.baz.a' => 'b' + ], $array); } public function testUndot() From 62fb709b778f067499c9f8d81de12fc93f93cb07 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 09:17:13 +0100 Subject: [PATCH 3/8] Fix cs --- tests/Support/SupportArrTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 3a63f24b7c60..a9e60a972f1f 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -119,14 +119,14 @@ public function testDot() 'user.name' => 'Taylor', 'user.age' => 25, 'user.languages.0' =>'PHP', - 'user.languages.1' => 'C#' + 'user.languages.1' => 'C#', ], $array); $array = Arr::dot(['foo', 'foo' => ['bar' => 'baz', 'baz' => ['a' => 'b']]]); $this->assertEquals([ 'foo', 'foo.bar' => 'baz', - 'foo.baz.a' => 'b' + 'foo.baz.a' => 'b', ], $array); } From c53eb647473930cce9c8f86a20ee6270b01156b8 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 10:20:06 +0100 Subject: [PATCH 4/8] Use array_merge spread to increase speed and decrease memory usage --- src/Illuminate/Collections/Arr.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 4b44c4162218..00834c04d080 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -109,17 +109,17 @@ public static function divide($array) */ public static function dot($array, $prepend = '') { - $results = []; + $results = [[]]; foreach ($array as $key => $value) { if (is_array($value) && ! empty($value)) { - $results += static::dot($value, $prepend.$key.'.'); + $results[] = static::dot($value, $prepend.$key.'.'); } else { - $results[$prepend.$key] = $value; + $results[0][$prepend.$key] = $value; } } - return $results; + return array_merge(...$results); } /** From d653327cf8be5336b7fdd35e52e3ef38869a5bbe Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 10:37:11 +0100 Subject: [PATCH 5/8] Fix ordering --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 00834c04d080..c5e89bf62e02 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -115,7 +115,7 @@ public static function dot($array, $prepend = '') if (is_array($value) && ! empty($value)) { $results[] = static::dot($value, $prepend.$key.'.'); } else { - $results[0][$prepend.$key] = $value; + $results[][$prepend.$key] = $value; } } From 2b3e588b3923a5b6854a7838428ad417109dd17e Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 17:10:31 +0100 Subject: [PATCH 6/8] Change assertEquals to assertSame in order to verify that ordering is consistant --- tests/Support/SupportArrTest.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index a9e60a972f1f..159086ce1ad3 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Support; use ArrayObject; +use Illuminate\Foundation\Console\ConfigShowCommand; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; @@ -100,22 +101,22 @@ public function testDivide() public function testDot() { $array = Arr::dot(['foo' => ['bar' => 'baz']]); - $this->assertEquals(['foo.bar' => 'baz'], $array); + $this->assertSame(['foo.bar' => 'baz'], $array); $array = Arr::dot([]); - $this->assertEquals([], $array); + $this->assertSame([], $array); $array = Arr::dot(['foo' => []]); - $this->assertEquals(['foo' => []], $array); + $this->assertSame(['foo' => []], $array); $array = Arr::dot(['foo' => ['bar' => []]]); - $this->assertEquals(['foo.bar' => []], $array); + $this->assertSame(['foo.bar' => []], $array); $array = Arr::dot(['name' => 'taylor', 'languages' => ['php' => true]]); - $this->assertEquals(['name' => 'taylor', 'languages.php' => true], $array); + $this->assertSame(['name' => 'taylor', 'languages.php' => true], $array); $array = Arr::dot(['user' => ['name' => 'Taylor', 'age' => 25, 'languages' => ['PHP', 'C#']]]); - $this->assertEquals([ + $this->assertSame([ 'user.name' => 'Taylor', 'user.age' => 25, 'user.languages.0' =>'PHP', @@ -123,11 +124,19 @@ public function testDot() ], $array); $array = Arr::dot(['foo', 'foo' => ['bar' => 'baz', 'baz' => ['a' => 'b']]]); - $this->assertEquals([ + $this->assertSame([ 'foo', 'foo.bar' => 'baz', 'foo.baz.a' => 'b', ], $array); + + $array = Arr::dot(['foo' => 'bar', 'empty_array' => [], 'user' => ['name' => 'Taylor'], 'key' => 'value']); + $this->assertSame([ + 'foo' => 'bar', + 'empty_array' => [], + 'user.name' => 'Taylor', + 'key' => 'value', + ], $array); } public function testUndot() From b0549916a1e3cf41156951c9a4b8a1cbd993df08 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 17:14:02 +0100 Subject: [PATCH 7/8] Explicit array creation --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index c5e89bf62e02..e857e1ae18bd 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -115,7 +115,7 @@ public static function dot($array, $prepend = '') if (is_array($value) && ! empty($value)) { $results[] = static::dot($value, $prepend.$key.'.'); } else { - $results[][$prepend.$key] = $value; + $results[] = [$prepend.$key => $value]; } } From adfdaaa3f2a63db1ad0743df60bde12998975c9b Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 15 Dec 2023 17:16:16 +0100 Subject: [PATCH 8/8] Fix cs --- tests/Support/SupportArrTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 159086ce1ad3..a539eb4b2b02 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -3,7 +3,6 @@ namespace Illuminate\Tests\Support; use ArrayObject; -use Illuminate\Foundation\Console\ConfigShowCommand; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; use Illuminate\Support\Collection;