Repository: symfony/symfony
File: src/Symfony/Component/Form/Util/OrderedHashMap.php (PHP)

Code before:
    {
        return isset($this->elements[$key]);
    }

    public function offsetGet(mixed $key): mixed
    {
        if (!isset($this->elements[$key])) {
            throw new \OutOfBoundsException(\sprintf('The offset "%s" does not exist.', $key));
        }

        return $this->elements[$key];
    }

    public function offsetSet(mixed $key, mixed $value): void
    {
        if (null === $key || !isset($this->elements[$key])) {
            if (null === $key) {
                // Imitate PHP behavior of generating a key that equals
                // the highest existing integer key + 1
                $max = -1;
                foreach ($this->elements as $k => $v) {
                    if (\is_int($k) && $k > $max) {
                        $max = $k;
                    }
                }
                $key = $max + 1;
            }

            $this->orderedKeys[] = (string) $key;
        }

        $this->elements[$key] = $value;
    }

    public function offsetUnset(mixed $key): void
    {
        if (false !== ($position = array_search((string) $key, $this->orderedKeys))) {
            array_splice($this->orderedKeys, $position, 1);
            unset($this->elements[$key]);

            foreach ($this->managedCursors as $i => $cursor) {
                if ($cursor >= $position) {
                    --$this->managedCursors[$i];
                }
            }
        }
    }

    public function getIterator(): \Traversable
    {
        return new OrderedHashMapIterator($this->elements, $this->orderedKeys, $this->managedCursors);

Review comment (suggestion):
this should work better:
```suggestion
                $this->elements[] = $value;
                $key = array_key_last($this->elements);
```

Code after:
    {
        return isset($this->elements[$key]);
    }

    public function offsetGet(mixed $key): mixed
    {
        if (!isset($this->elements[$key])) {
            throw new \OutOfBoundsException(\sprintf('The offset "%s" does not exist.', $key));
        }

        return $this->elements[$key];
    }

    public function offsetSet(mixed $key, mixed $value): void
    {
        if (null === $key) {
            $this->elements[] = $value;
            $key = array_key_last($this->elements);
            $this->orderedKeys[] = (string) $key;
        } elseif (!\array_key_exists($key, $this->elements)) {
            $this->orderedKeys[] = (string) $key;
        }

        $this->elements[$key] = $value;
    }

    public function offsetUnset(mixed $key): void
    {
        if (false !== ($position = array_search((string) $key, $this->orderedKeys))) {
            array_splice($this->orderedKeys, $position, 1);
            unset($this->elements[$key]);

            foreach ($this->managedCursors as $i => $cursor) {
                if ($cursor >= $position) {
                    --$this->managedCursors[$i];
                }
            }
        }
    }

    public function getIterator(): \Traversable
    {
        return new OrderedHashMapIterator($this->elements, $this->orderedKeys, $this->managedCursors);
    }

    public function count(): int
    {
        return \count($this->elements);
    }
}