Skip to content

Commit ab2b186

Browse files
authored
Merge pull request #5940 from kenjis/fix-dot_array_search
fix: `dot_array_search()` unexpected behavior
2 parents c3fd096 + be1610b commit ab2b186

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

system/Helpers/array_helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ function _array_search_dot(array $indexes, array $array)
8989
return _array_search_dot($indexes, $array[$currentIndex]);
9090
}
9191

92-
// Otherwise we've found our match!
93-
return $array[$currentIndex];
92+
// Otherwise, not found.
93+
return null;
9494
}
9595
}
9696

tests/system/Helpers/ArrayHelperTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ public function testArrayDotTooManyLevels()
4545
],
4646
];
4747

48-
$this->assertSame(23, dot_array_search('foo.bar.baz', $data));
48+
$this->assertNull(dot_array_search('foo.bar.baz', $data));
49+
}
50+
51+
public function testArrayDotTooManyLevelsWithWildCard()
52+
{
53+
$data = [
54+
'a' => [],
55+
];
56+
57+
$this->assertNull(dot_array_search('a.*.c', $data));
4958
}
5059

5160
/**
@@ -77,15 +86,24 @@ public function testArrayDotEscape()
7786

7887
public function testArraySearchDotMultiLevels()
7988
{
80-
$data1 = ['bar' => [['foo' => 'baz']]];
81-
$data2 = ['bar' => [
82-
['foo' => 'bizz'],
83-
['foo' => 'buzz'],
84-
]];
85-
$data3 = ['baz' => 'none'];
86-
89+
$data1 = [
90+
'bar' => [
91+
['foo' => 'baz'],
92+
],
93+
];
8794
$this->assertSame('baz', dot_array_search('bar.*.foo', $data1));
95+
96+
$data2 = [
97+
'bar' => [
98+
['foo' => 'bizz'],
99+
['foo' => 'buzz'],
100+
],
101+
];
88102
$this->assertSame(['bizz', 'buzz'], dot_array_search('bar.*.foo', $data2));
103+
104+
$data3 = [
105+
'baz' => 'none',
106+
];
89107
$this->assertNull(dot_array_search('bar.*.foo', $data3));
90108
}
91109

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ BREAKING
2222
- The color code output by :ref:`CLI::color() <cli-library-color>` has been changed to fix a bug.
2323
- 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.
2424
- There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method.
25+
- 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``.
2526

2627
Enhancements
2728
************

user_guide_src/source/helpers/array_helper.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ The following functions are available:
4848

4949
.. literalinclude:: array_helper/005.php
5050

51+
.. note:: Prior to v4.2.0, ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returned ``23``
52+
due to a bug. v4.2.0 and later returns ``null``.
53+
5154
.. php:function:: array_deep_search($key, array $array)
5255
5356
:param mixed $key: The target key

0 commit comments

Comments
 (0)