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

Casting to int something that's already an int #3884

Closed
Synchro opened this issue Sep 25, 2020 · 5 comments · Fixed by phpstan/phpstan-src#326
Closed

Casting to int something that's already an int #3884

Synchro opened this issue Sep 25, 2020 · 5 comments · Fixed by phpstan/phpstan-src#326

Comments

@Synchro
Copy link

Synchro commented Sep 25, 2020

Bug report

This is a duplicate of #1251, but with a better use case.

phpstan reports Casting to int something that's already an int even when the something has a possibility of not being an int.

In my case this occurs with bindec, which has a documented return type of float|int, and I need to guarantee that it's an int, so I'm casting it (it's safe because I'm also limiting the input size to 3 hex chars, so it will never actually hit the overflow that would cause it to return a float).

The cast is also needed for other type checkers such as psalm.

Code snippet that reproduces the problem

$chunk = '123';
$offset = (int)bindec(base_convert($chunk, 16, 2));

phpstan's opinion would be correct if it could know all possible values of $chunk, but that's hard to know. For example FFFFFFFFFFFFFFFF would return an int from bindec, but FFFFFFFFFFFFFFFFF would return a float.

Expected output

No error

@mergeable
Copy link

mergeable bot commented Sep 25, 2020

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

This is just a question of wrong bindec signature, in that case follow this: https://github.com/phpstan/phpstan/issues/new?template=Function_signature_mismatch.md

Thanks.

@Synchro
Copy link
Author

Synchro commented Sep 25, 2020

But the signature is not wrong? bindec can return a float or an int?

@Synchro
Copy link
Author

Synchro commented Sep 25, 2020

Duh, sorry, wrong end of stick.

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

Successfully merging a pull request may close this issue.

2 participants