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

ArgumentTypeCoercion for typecasted variables #6966

Closed
kkmuffme opened this issue Nov 22, 2021 · 4 comments · Fixed by #7071 or #8366
Closed

ArgumentTypeCoercion for typecasted variables #6966

kkmuffme opened this issue Nov 22, 2021 · 4 comments · Fixed by #7071 or #8366
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue internal stubs/callmap

Comments

@kkmuffme
Copy link
Contributor

When a variable is typecasted and clearly is a positive int, psalm thinks it's generic int:

https://psalm.dev/r/c1e0a9efd4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c1e0a9efd4
<?php

$time = (int) round( 1.23 );
sleep( $time );

$time = (int) '123';
sleep( $time );
Psalm output (using commit 9943efb):

ERROR: ArgumentTypeCoercion - 4:8 - Argument 1 of sleep expects 0|positive-int, parent type int provided

ERROR: ArgumentTypeCoercion - 7:8 - Argument 1 of sleep expects 0|positive-int, parent type int provided

@orklah
Copy link
Collaborator

orklah commented Nov 22, 2021

These are two easy issues.

The one with round must be fixed by a return type provider. We already have a few of them, they're easy to create. You can look at https://github.com/vimeo/psalm/blob/6abce3525ac34f815aaa3b7953739b744ca5c39e/src/Psalm/Internal/Provider/ReturnTypeProvider/StrReplaceReturnTypeProvider.php for an example

The one with just the case must be fixed in CastAnalyzer. Most specifically, in this condition:

if ($stmt instanceof PhpParser\Node\Expr\Cast\Int_) {

Here, we could look at the Union content, if we have a single string and if it's a numeric, we could just cast to int in Psalm and return a literal int with this value instead of returning the generic int

@orklah orklah added easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue internal stubs/callmap labels Nov 22, 2021
ricardoboss added a commit to ricardoboss/psalm that referenced this issue Dec 5, 2021
ricardoboss added a commit to ricardoboss/psalm that referenced this issue Dec 6, 2021
ricardoboss added a commit to ricardoboss/psalm that referenced this issue Jan 2, 2022
ricardoboss added a commit to ricardoboss/psalm that referenced this issue Jan 16, 2022
orklah pushed a commit that referenced this issue Jan 16, 2022
* Fixed #6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
@orklah
Copy link
Collaborator

orklah commented Jan 30, 2022

Half of the issue was resolved

@kkmuffme
Copy link
Contributor Author

kkmuffme commented Sep 9, 2022

Second half has PR created, #8366

kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 11, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 11, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 11, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 15, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 15, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 15, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 19, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 19, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
kkmuffme pushed a commit to kkmuffme/psalm that referenced this issue Sep 19, 2022
…meo#6966) (vimeo#7071)

* Fixed vimeo#6966

* Only accept >= 0 values for mode argument in round()

* Made round() only return float or literal float values and remove unneeded test

* Registered RoundReturnTypeProvider

* Updated cast analyzer to handle single string literal int values as literal ints

* Fixed psalm errors

* Fix invalid property accesses

* Addressed comments

* Added Tests

* Marked RoundReturnTypeProvider as internal

* Fixed CS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue internal stubs/callmap
Projects
None yet
2 participants