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

Try to provide literal int types when possible (fixes #6966) #7071

Conversation

ricardoboss
Copy link
Contributor

@ricardoboss ricardoboss commented Dec 5, 2021

This PR fixes #6966.

I added a return type provider for the round function, which will now try to return a literal float, which can then be used for further analysis.
The second change involves modifying the CastAnalyzer to also return a literal int when possible. I also tried to simplify the code a bit.

@orklah
Copy link
Collaborator

orklah commented Dec 5, 2021

Thanks!

You have to enable the return type provider in FunctionReturnTypeProvider first

@orklah
Copy link
Collaborator

orklah commented Dec 5, 2021

I made an error in my explanation. round only returns floats:

https://3v4l.org/ETvDv

That shouldn't be an issue however, what you have to do to fix #6966 is check if you have a TLiteralInt or a TLiteralFloat in the num_arg. If it is, you should return a TLiteralFloat with the rounded value of what was in num_arg.

@ricardoboss
Copy link
Contributor Author

I need to implement the CastAnalyzer too. So this is should really be a draft PR.

@ricardoboss ricardoboss changed the title Fixed vimeo/psalm#6966 WIP: Fix vimeo/psalm#6966 Dec 5, 2021
@ricardoboss ricardoboss changed the title WIP: Fix vimeo/psalm#6966 Try to provide literal int types when possible (fixes #6966) Dec 5, 2021
@ricardoboss
Copy link
Contributor Author

I'm struggling to get the actual values with which the round method was called. As you can see, I'm trying to get them with $call_args[1]->value, which gives a PhpParser\Node\Expr. How do I get the actual value?

@orklah
Copy link
Collaborator

orklah commented Dec 5, 2021

try a $statements_analyzer->node_data->getType(...) on that. It may be a complex expression but Psalm should already have inferred it

@weirdan
Copy link
Collaborator

weirdan commented Dec 5, 2021

E.g.

$first_arg_type = isset($call_args[0]) ? $statements_source->node_data->getType($call_args[0]->value) : null;
$second_arg_type = isset($call_args[1]) ? $statements_source->node_data->getType($call_args[1]->value) : null;
$third_arg_type = isset($call_args[2]) ? $statements_source->node_data->getType($call_args[2]->value) : null;

@ricardoboss ricardoboss force-pushed the issue-6966-argumenttypecoercion_for_typecasted_vars branch from 5872f66 to 415eb81 Compare December 6, 2021 01:01
@orklah
Copy link
Collaborator

orklah commented Dec 8, 2021

@ricardoboss is this ready for review?

@ricardoboss
Copy link
Contributor Author

Yes! Sorry I didn't notify you...

@orklah
Copy link
Collaborator

orklah commented Dec 8, 2021

No worries, we'll check that :)

@ricardoboss
Copy link
Contributor Author

@orklah can you take a look again?

Copy link
Collaborator

@orklah orklah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above. Can you add some tests too?

@orklah orklah marked this pull request as draft December 14, 2021 23:53
@orklah
Copy link
Collaborator

orklah commented Dec 29, 2021

@ricardoboss do you need some help to design the tests?

Could you also rebase to solve the conflicts?

@ricardoboss
Copy link
Contributor Author

Will do a rebase! I haven't had time to write tests yet. Haven't forgot it either.

@ricardoboss ricardoboss force-pushed the issue-6966-argumenttypecoercion_for_typecasted_vars branch from 34ac2e7 to d3fc5b6 Compare January 2, 2022 17:52
@orklah
Copy link
Collaborator

orklah commented Jan 15, 2022

Sorry for not coming back to this earlier, I don't get notifications for commits so I didn't see this change. I'll take a look. Don't hesitate to ping me or leave a comment when you change things

@orklah orklah added the release:feature The PR will be included in 'Features' section of the release notes label Jan 15, 2022
@orklah orklah marked this pull request as ready for review January 15, 2022 10:47
@orklah
Copy link
Collaborator

orklah commented Jan 15, 2022

Seems fine. Can you rebase and fix the Code Style error please?

@ricardoboss
Copy link
Contributor Author

Can you write a test for this? I would like to see how you test this behavior.

I will do a rebase when I get time to do so

@orklah
Copy link
Collaborator

orklah commented Jan 15, 2022

For the cast one, you can add a test in ValueTest.php in providerValidCodeParse with the code: $a = (int)'5'; and in the assertions you can check $a=== => '5'

For the round one, you can add a test in FunctionCallTest.php in providerValidCodeParse with the code $a = round(5.111, 2); and in the assertions you can check $a=== => 'float(5.11)'

@ricardoboss ricardoboss force-pushed the issue-6966-argumenttypecoercion_for_typecasted_vars branch from d3fc5b6 to 62a0923 Compare January 16, 2022 20:10
@orklah
Copy link
Collaborator

orklah commented Jan 16, 2022

Thanks!

@orklah orklah merged commit 26dd4c5 into vimeo:master Jan 16, 2022
@ricardoboss ricardoboss deleted the issue-6966-argumenttypecoercion_for_typecasted_vars branch January 16, 2022 20:34
@kkmuffme
Copy link
Contributor

kkmuffme commented Sep 9, 2022

The missing cast analyzer things are added in this PR now #8366 and this PR is cherry-picked to 4.x

kkmuffme pushed a commit to kkmuffme/psalm that referenced this pull request 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 pull request 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 pull request 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 pull request 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 pull request 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 pull request 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 pull request 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 pull request 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 pull request 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
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ArgumentTypeCoercion for typecasted variables
4 participants