Closed
Description
Symfony version(s) affected: v5.3.2
Description
When submitting a form with square brackets after the variable name, PHP parses it into an array.
The type hint in \Symfony\Component\HttpFoundation\InputBag::get
is set to:
@return string|int|float|bool|null
while the method will can also return an array
.
How to reproduce
Require symfony/http-foundation
in your composer.json
Create an index.html
file:
<form method="POST" action="request.php">
<input type="text" name="settings[first]">
<input type="text" name="settings[second]">
<input type="submit">
</form>
Create a request.php
file:
<?php
use Symfony\Component\HttpFoundation\Request;
require_once __DIR__. "/vendor/autoload.php";
$r = Request::createFromGlobals();
echo "<pre>";
print_r($r->request->get("settings"));
echo "</pre>";
After filling out the form with values "1" and "2" it will display:
Array
(
[first] => 1
[second] => 2
)
Possible Solution
The docblock could be extended to:
@return string|int|float|bool|null|array
Additional context
See https://www.php.net/manual/en/faq.html.php#faq.html.arrays
I found this because Psalm gave me an error:
ERROR: InvalidArrayAccess Cannot access array value on non-array variable $r of type scalar (see https://psalm.dev/005)