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 incorrectly states BCMath functions can return null in PHP 8 project #5134

Closed
khkramer opened this issue Jun 3, 2021 · 8 comments
Closed
Labels
Milestone

Comments

@khkramer
Copy link

khkramer commented Jun 3, 2021

Bug report

In a PHP 8 only codebase PHPStan currently states: Function divide() should return string but returns string|null.
It only seems to happen when strict typing is declared in the php file.

While PHP 8 bcmul and bcdiv either return a numeric string or throw an exception when dividing by zero. ( According to the Jetbrains stubs and which I have verified )

Code snippet that reproduces the problem

https://phpstan.org/r/376c482f-e055-400f-b8f5-d212d2ca1b9d

Expected output

I would have expected it to not have emitted an error in this instance.
I have also looked at the phpstan-src repository and I saw that the correct return types are present in one of the delta files, but I can't figure out why they aren't used in my instance.

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

In general I'm very happy with PHPStan providing some stability in the unsafe world that is PHP :)

@mergeable
Copy link

mergeable bot commented Jun 3, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@khkramer
Copy link
Author

khkramer commented Jun 3, 2021

Updated with link to reproduce.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 3, 2021
@ondrejmirtes
Copy link
Member

@khkramer
Copy link
Author

khkramer commented Jun 4, 2021

I'm working on a pull request, however there are a few test cases present in the test file ( tests/PHPStan/Analyser/data/bcmath-dynamic-return.php ) which don't apply for PHP 8 or higher either.

What is the preferred way to make an exception for these cases?

@ondrejmirtes
Copy link
Member

It should be tested in NodeScopeResolverTest. You can yield from different files based on PHP_VERSION_ID.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-10: Function divide() should return string but returns string|null.
+ 6: Function divide() should return string but return statement is missing.
Full report
Line Error
6 Function divide() should return string but return statement is missing.

@ondrejmirtes
Copy link
Member

Fixed by phpstan/phpstan-src#1152

@github-actions
Copy link

github-actions bot commented May 6, 2022

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 May 6, 2022
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