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

PHPStan wrongly assumes that $_SERVER cannot be null #10870

Open
sebastianbergmann opened this issue Apr 13, 2024 · 2 comments
Open

PHPStan wrongly assumes that $_SERVER cannot be null #10870

sebastianbergmann opened this issue Apr 13, 2024 · 2 comments
Labels

Comments

@sebastianbergmann
Copy link
Contributor

Bug report

In PHPUnit, I have this line of code:

$env = $_SERVER ?? [];

PHPStan wrongfully complains about this line:

Variable $_SERVER on left side of ?? always exists and is not nullable.

However, this line of code exists for a reason: some people have test code or test code that sets $_SERVER to null. See https://3v4l.org/t1PTW for proof that this is possible.

Code snippet that reproduces the problem

https://phpstan.org/r/fa40be63-50df-4254-86e8-d02f26654e49

Expected output

No error.

Did PHPStan help you today? Did it make you happy in any way?

Yes!

@ondrejmirtes
Copy link
Member

This is a hard issue to tackle correctly and practically. Apparently you can set these global variables to anything: https://3v4l.org/8H9ap

So ideally we should make them just mixed because it's possible they might be anything.

But if you're in a codebase that doesn't do anything weird like this, they're going to be array<string, mixed>. So complaining that you're passing mixed into array after such a fix would be annoying for most people.

We could also suppress some errors reported to these global variables when rechecking their type, but it's hard to do such a thing consistently without changing type of these variables.

I'm going to keep this open but I'd say the best course of action is to ignore this kind of error - "hey, I know what I'm doing, I'm going to ignore this here".

@sebastianbergmann
Copy link
Contributor Author

"hey, I know what I'm doing, I'm going to ignore this here"

Okay :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants