8000 Fixed unsetting from loosely equal keys OrderedHashMap · symfony/symfony@ba37cba · GitHub
[go: up one dir, main page]

Skip to content

Commit ba37cba

Browse files
committed
Fixed unsetting from loosely equal keys OrderedHashMap
1 parent e980335 commit ba37cba

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public function testInsertNullKeys()
5656
$this->assertSame(array(0 => 1, 'foo' => 2, 1 => 3), iterator_to_array($map));
5757
}
5858

59+
public function testInsertLooselyEqualKeys()
60+
{
61+
$map = new OrderedHashMap();
62+
$map['1 as a string'] = '1 as a string';
63+
$map[1] = 1;
64+
65+
$this->assertSame(array('1 as a string' => '1 as a string', 1 => 1), iterator_to_array($map));
66+
}
67+
5968
/**
6069
* Updates should not change the position of an element, otherwise we could
6170
* turn foreach loops into endless loops if they change the current
@@ -111,6 +120,17 @@ public function testUnset()
111120
$this->assertSame(array('second' => 2), iterator_to_array($map));
112121
}
113122

123+
public function testUnsetFromLooselyEqualKeysHashMap()
124+
{
125+
$map = new OrderedHashMap();
126+
$map['1 as a string'] = '1 as a string';
127+
$map[1] = 1;
128+
129+
unset($map[1]);
130+
131+
$this->assertSame(array('1 as a string' => '1 as a string'), iterator_to_array($map));
132+
}
133+
114134
public function testUnsetNonExistingSucceeds()
115135
{
116136
$map = new OrderedHashMap();

src/Symfony/Component/Form/Util/OrderedHashMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function offsetSet($key, $value)
133133
: 1 + (int) max($this->orderedKeys);
134134
}
135135

136-
$this->orderedKeys[] = $key;
136+
$this->orderedKeys[] = (string) $key;
137137
}
138138

139139
$this->elements[$key] = $value;
@@ -144,7 +144,7 @@ public function offsetSet($key, $value)
144144
*/
145145
public function offsetUnset($key)
146146
{
147-
if (false !== ($position = array_search($key, $this->orderedKeys))) {
147+
if (false !== ($position = array_search((string) $key, $this->orderedKeys))) {
148148
array_splice($this->orderedKeys, $position, 1);
149149
unset($this->elements[$key]);
150150

src/Symfony/Component/Form/Util/OrderedHashMapIterator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ public function next()
118118
*/
119119
public function key()
120120
{
121-
return $this->key;
121+
if (null === $this->key) {
122+
return null;
123+
}
124+
125+
$array = array($this->key => null);
126+
127+
return key($array);
122128
}
123129

124130
/**

0 commit comments

Comments
 (0)
0