Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 45e677e

Browse filesBrowse files
committed
bug #24566 Fixed unsetting from loosely equal keys OrderedHashMap (maryo)
This PR was merged into the 2.7 branch. Discussion ---------- Fixed unsetting from loosely equal keys OrderedHashMap | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24558 | License | MIT Commits ------- ba37cba Fixed unsetting from loosely equal keys OrderedHashMap
2 parents ff45992 + ba37cba commit 45e677e
Copy full SHA for 45e677e

File tree

3 files changed

+29
-3
lines changed
Filter options

3 files changed

+29
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Util/OrderedHashMapTest.php
+20Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Util/OrderedHashMap.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Util/OrderedHashMapIterator.php
+7-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.