8000 [HttpFoundation] Mising array return type in Symfony\Component\HttpFoundation\InputBag::get · Issue #41800 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[HttpFoundation] Mising array return type in Symfony\Component\HttpFoundation\InputBag::get #41800
Closed
@gdejong

Description

@gdejong

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0