Skip to content

InputBag::get() method incorrectly reports type error for bool default parameter #453

@sfmok

Description

@sfmok

PHPStan is incorrectly reporting a type error when using false as the default parameter for Symfony\Component\HttpFoundation\InputBag::get() method.

The error states that the method expects string|null but false is given, which is incorrect according to the actual method signature.

PHPStan reports the following error:

Parameter #2 $default of method Symfony\Component\HttpFoundation\InputBag<string>::get() expects string|null, false given.

Code Example

use Symfony\Component\HttpFoundation\Request;

$request = new Request();
$isPreview = $request->query->get('preview', false); // This triggers the error

Environment Details

  • PHPStan Version: 1.12.25
  • PHPStan Symfony Extension: 1.4.15
  • Symfony Version: 6.4.*
  • PHP Version: 8.2.20

The actual InputBag::get() method signature is:

/**
 * Returns a scalar input value by name.
 *
 * @param string|int|float|bool|null $default The default value if the input key does not exist
 */
public function get(string $key, mixed $default = null): string|int|float|bool|null
{
    if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
        throw new \InvalidArgumentException(sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
    }

    $value = parent::get($key, $this);

    if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
        throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
    }

    return $this === $value ? $default : $value;
}

This appears to be an issue with PHPStan's Symfony extension having an outdated or incorrect type definition for the InputBag::get() method. The extension seems to be using an older type definition that doesn't match the current Symfony implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions