Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit c5bb1ed

Browse files
committed
adding regression tests for a bug when inserting a value for a/b/c when the array is empty
1 parent 578966a commit c5bb1ed

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/ArrayPath.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public static function set(&$aSource, $mIndex, $mValue)
8888
$mCurSource = &$aSource;
8989

9090
foreach ($aPath as $mPath) {
91-
if (array_key_exists($mPath, (array) $mCurSource)) {
92-
$mCurSource = &$mCurSource[$mPath];
93-
} else {
91+
if (!array_key_exists($mPath, (array) $mCurSource)) {
9492
$mCurSource[$mPath] = array();
9593
}
94+
95+
$mCurSource = &$mCurSource[$mPath];
9696
}
9797

98-
$mCurSource[$mPath] = $mValue;
98+
$mCurSource = $mValue;
9999

100100
return $mValue;
101101
}

test/src/ArrayPathTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ public function testSet()
126126
$this->assertEquals(17, ArrayPath::set($aSource, 'l1-2/l2-2/l3-3', 17));
127127
$this->assertEquals($aExpected, $aSource);
128128
}
129+
130+
public function testSetWithoutParent()
131+
{
132+
$aSource = array();
133+
134+
$aExpected = array(
135+
'l1-2' => array(
136+
'l2-2' => array(
137+
'l3-3' => 17
138+
)
139+
)
140+
);
141+
$this->assertEquals(17, ArrayPath::set($aSource, 'l1-2/l2-2/l3-3', 17));
142+
$this->assertEquals($aExpected, $aSource);
143+
}
129144

130145
public function testRemove()
131146
{

0 commit comments

Comments
 (0)