phpstan/phpstan-symfony

Type inference for HeaderBag could support has()

Seldaek opened this issue · 3 comments

Right now calling this triggers an error as the returned header is string|null, but really after ->has($key) returns true, we can assert that ->get($sameKey) will be string. At least as long as there is no other impure call made in between like ->remove() or ->replace()

if ($request->headers->has('foo')) {
    $v = strtolower($request->headers->get('foo'));
}

Hi @Seldaek,

This is untrue.

The has method is based on array_key_exists, which means you can still have a NULL headers.

public function has(string $key): bool
    {
        return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all());
    }

The same issue exists for InputBag/ParameterBag ; I opened #409

Yeah I guess you're right from an implementation standpoint. I don't know what would ever cause a header to receive a null value in the real world though, but definitely could happen in tests etc..

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.