Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference for HeaderBag could support has() #399

Closed
Seldaek opened this issue Jun 26, 2024 · 3 comments
Closed

Type inference for HeaderBag could support has() #399

Seldaek opened this issue Jun 26, 2024 · 3 comments

Comments

@Seldaek
Copy link

Seldaek commented Jun 26, 2024

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'));
}
@VincentLanglet
Copy link
Contributor

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

@Seldaek
Copy link
Author

Seldaek commented Sep 26, 2024

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..

@Seldaek Seldaek closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2024
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants