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

False positive: expects int, int|null given after If condition #4177

Closed
oldy777 opened this issue Dec 3, 2020 · 8 comments
Closed

False positive: expects int, int|null given after If condition #4177

oldy777 opened this issue Dec 3, 2020 · 8 comments

Comments

@oldy777
Copy link

oldy777 commented Dec 3, 2020

Bug report

PHPStan - PHP Static Analysis Tool 0.12.56

I get an error "expects int, int|null given" despite the fact that I checked it before
if I checking clearly !== null everything works good
 

Code snippet that reproduces the problem

https://phpstan.org/r/d4e9e96b-9902-4da4-a85a-ae487094aa68

Expected output "No errors"

@ondrejmirtes
Copy link
Member

I'd argue that both calls on lines 24 and 28 shold report an error, because of rand().

@oldy777
Copy link
Author

oldy777 commented Dec 3, 2020

@ondrejmirtes Sorry, didn't catch what the problem with rand() ?
p.s. it's only for example here :)

@ondrejmirtes
Copy link
Member

The main problem here is that you rely on multiple calls to $request->getPeriodFrom() to return the same thing, but it might not be that way. It's much better to use variables: https://phpstan.org/r/838f94c8-098c-4319-bb07-3a573fd1500c

@oldy777
Copy link
Author

oldy777 commented Dec 3, 2020

I got it.
But in my code I consider requestDto like immutable. That's why add variables looks like overhead

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 (1 error)
-==========
-
-24: Parameter #1 $timestamp of method DateTime::setTimestamp() expects int, int|null given.
-
-PHP 7.1 – 7.4 (1 error)
-==========
-
-24: Parameter #1 $unixtimestamp of method DateTime::setTimestamp() expects int, int|null given.
+24: Parameter #1 $timestamp of method DateTime::setTimestamp() expects int, int|null given.
Full report
Line Error
24 `Parameter #1 $timestamp of method DateTime::setTimestamp() expects int, int

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 (1 error)
-==========
-
-24: Parameter #1 $timestamp of method DateTime::setTimestamp() expects int, int|null given.
-
-PHP 7.1 – 7.4 (1 error)
-==========
-
-24: Parameter #1 $unixtimestamp of method DateTime::setTimestamp() expects int, int|null given.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@d4edc59

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

No branches or pull requests

3 participants