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 thinks static variable can be float #1870

Closed
iluuu1994 opened this issue Feb 3, 2019 · 10 comments
Closed

PHPStan thinks static variable can be float #1870

iluuu1994 opened this issue Feb 3, 2019 · 10 comments

Comments

@iluuu1994
Copy link
Contributor

iluuu1994 commented Feb 3, 2019

PHPStan 0.11

Summary of a problem or a feature request

I don't understand this one. Making a local static variable makes PHPStan think the variable can be a float when there's really no reason to. Removing the static removes the error.

Code snippet that reproduces the problem

function returnsInt(): int
{
	static $i = 0;
	return ++$i;
}

Function returnsInt() should return int but returns float|int.

https://phpstan.org/r/249d5f01-7caf-406d-bf12-5f6b116c3810

Expected output

No error.

@ondrejmirtes
Copy link
Member

It's because any static variable is mixed. mixed++ is float|int. I should fix this by supporting @var above static assignment: https://phpstan.org/r/282b0f74-83b1-4380-b4f3-8074f1b3e426

@ondrejmirtes ondrejmirtes added this to the NodeScopeResolver bugs milestone Feb 3, 2019
@iluuu1994
Copy link
Contributor Author

@ondrejmirtes I see. So basically we assume mixed because tracking the value is hard, right? I guess since this is so rare it's not really worth fixing. @var seems like a pragmatic solution.

@ondrejmirtes
Copy link
Member

I'm currently rewriting NodeScopeResolver so the tracking might be worth revisiting :)

@ondrejmirtes
Copy link
Member

@var above static is already supported: https://phpstan.org/r/7d2742f1-d521-4767-8723-85a7fbf26f62

@iluuu1994
Copy link
Contributor Author

And coincidentally, this is correct, int++ also technically results in int|float ^^ https://3v4l.org/9barY
But this edge case is probably worth ignoring.

@phpstan-bot
Copy link
Contributor

@ondrejmirtes PHPStan now reports different result with your code snippet:

@@ @@
-7: Function returnsInt() should return int but returns float|int.
+No errors

@phpstan-bot
Copy link
Contributor

@iluuu1994 After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-6: Function returnsInt() should return int but returns float|int.
+No errors

@iluuu1994
Copy link
Contributor Author

Nice 😁 Thanks @ondrejmirtes!

@ondrejmirtes
Copy link
Member

I love this bot :) You're welcome!

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Sep 1, 2021
@github-actions
Copy link

github-actions bot commented Oct 3, 2021

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 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants