Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e7c9fb

Browse files
bug #63144 [Form] Fix OrderedHashMap auto-increment logic with mixed keys (yoeunes)
This PR was merged into the 6.4 branch. Discussion ---------- [Form] Fix `OrderedHashMap` auto-increment logic with mixed keys | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT When `OrderedHashMap` has mixed integer and string keys, appending a new element overwrites existing integer keys instead of generating a new key: ```php $map = new OrderedHashMap(); $map[0] = 'zero'; $map[1] = 'one'; $map['foo'] = 'bar'; $map[] = 'new'; // overwrites key 1 instead of creating key 2 ``` Commits ------- 10f9f01 [Form] Fix OrderedHashMap auto-increment logic with mixed keys
2 parents 6e00648 + 10f9f01 commit 0e7c9fb

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,36 @@ public function testCount()
524524

525525
$this->assertCount(2, $map);
526526
}
527+
528+
public function testAppendDoesNotOverwriteExistingNumericKeysWhenStringKeysExist()
529+
{
530+
$map = new OrderedHashMap();
531+
$map[0] = 'zero';
532+
$map[1] = 'one';
533+
$map['foo'] = 'bar';
534+
$map[] = 'new';
535+
536+
$this->assertSame('one', $map[1]);
537+
$this->assertSame('new', $map[2]);
538+
}
539+
540+
public function testUpdateNullValueDoesNotChangeElementPosition()
541+
{
542+
$map = new OrderedHashMap();
543+
$map['first'] = null;
544+
$map['second'] = 2;
545+
$map['first'] = null;
546+
547+
$this->assertSame(['first' => null, 'second' => 2], iterator_to_array($map));
548+
}
549+
550+
public function testUpdateFromNullValueDoesNotChangeElementPosition()
551+
{
552+
$map = new OrderedHashMap();
553+
$map['first'] = null;
554+
$map['second'] = 2;
555+
$map['first'] = 1;
556+
557+
$this->assertSame(['first' => 1, 'second' => 2], iterator_to_array($map));
558+
}
527559
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,11 @@ public function offsetGet(mixed $key): mixed
120120

121121
public function offsetSet(mixed $key, mixed $value): void
122122
{
123-
if (null === $key || !isset($this->elements[$key])) {
124-
if (null === $key) {
125-
$key = [] === $this->orderedKeys
126-
// If the array is empty, use 0 as key
127-
? 0
128-
// Imitate PHP behavior of generating a key that equals
129-
// the highest existing integer key + 1
130-
: 1 + (int) max($this->orderedKeys);
131-
}
132-
123+
if (null === $key) {
124+
$this->elements[] = $value;
125+
$key = array_key_last($this->elements);
126+
$this->orderedKeys[] = (string) $key;
127+
} elseif (!\array_key_exists($key, $this->elements)) {
133128
$this->orderedKeys[] = (string) $key;
134129
}
135130

0 commit comments

Comments
 (0)