From fa615965cce726300f5da4cae51bc3a363b3f6ec Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 30 Apr 2022 16:46:45 +0900 Subject: [PATCH 1/3] test: refactor for easier reading --- tests/system/Helpers/ArrayHelperTest.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 59257fbab7d6..6acb68edca66 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -77,15 +77,24 @@ public function testArrayDotEscape() public function testArraySearchDotMultiLevels() { - $data1 = ['bar' => [['foo' => 'baz']]]; - $data2 = ['bar' => [ - ['foo' => 'bizz'], - ['foo' => 'buzz'], - ]]; - $data3 = ['baz' => 'none']; - + $data1 = [ + 'bar' => [ + ['foo' => 'baz'], + ], + ]; $this->assertSame('baz', dot_array_search('bar.*.foo', $data1)); + + $data2 = [ + 'bar' => [ + ['foo' => 'bizz'], + ['foo' => 'buzz'], + ], + ]; $this->assertSame(['bizz', 'buzz'], dot_array_search('bar.*.foo', $data2)); + + $data3 = [ + 'baz' => 'none', + ]; $this->assertNull(dot_array_search('bar.*.foo', $data3)); } From 3d987fda85784f8794b5970f3fb77437e02cb30a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 30 Apr 2022 16:47:58 +0900 Subject: [PATCH 2/3] fix: unexpected behavior --- system/Helpers/array_helper.php | 4 ++-- tests/system/Helpers/ArrayHelperTest.php | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index b5350cdd8b70..9d2b44876cc9 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -89,8 +89,8 @@ function _array_search_dot(array $indexes, array $array) return _array_search_dot($indexes, $array[$currentIndex]); } - // Otherwise we've found our match! - return $array[$currentIndex]; + // Otherwise, not found. + return null; } } diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 6acb68edca66..50c6f94f4bfc 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -45,7 +45,16 @@ public function testArrayDotTooManyLevels() ], ]; - $this->assertSame(23, dot_array_search('foo.bar.baz', $data)); + $this->assertNull(dot_array_search('foo.bar.baz', $data)); + } + + public function testArrayDotTooManyLevelsWithWildCard() + { + $data = [ + 'a' => [], + ]; + + $this->assertNull(dot_array_search('a.*.c', $data)); } /** From be1610bd80f4a345bd694c93193f2b176d289198 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 4 May 2022 11:39:33 +0900 Subject: [PATCH 3/3] docs: add user guide --- user_guide_src/source/changelogs/v4.2.0.rst | 1 + user_guide_src/source/helpers/array_helper.rst | 3 +++ 2 files changed, 4 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 32129c30c4eb..b314dd892794 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -22,6 +22,7 @@ BREAKING - The color code output by :ref:`CLI::color() ` has been changed to fix a bug. - To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing. - There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method. +- The :php:func:`dot_array_search`'s unexpected behavior has been fixed. Now ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returns ``null``. The previous versions returned ``23``. Enhancements ************ diff --git a/user_guide_src/source/helpers/array_helper.rst b/user_guide_src/source/helpers/array_helper.rst index 845ccccf39b4..cf9517925234 100644 --- a/user_guide_src/source/helpers/array_helper.rst +++ b/user_guide_src/source/helpers/array_helper.rst @@ -48,6 +48,9 @@ The following functions are available: .. literalinclude:: array_helper/005.php +.. note:: Prior to v4.2.0, ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returned ``23`` + due to a bug. v4.2.0 and later returns ``null``. + .. php:function:: array_deep_search($key, array $array) :param mixed $key: The target key