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

support subtracted-mixed in toNumber() #1949

Merged
merged 2 commits into from Nov 5, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Nov 1, 2022

No description provided.

@staabm staabm marked this pull request as draft November 1, 2022 19:52
@staabm staabm force-pushed the mixed-to-number branch 2 times, most recently from c6b2f86 to 8c13b7c Compare November 1, 2022 20:01
@staabm staabm marked this pull request as ready for review November 1, 2022 20:13
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Comment on lines 410 to 415
if ($float->isSuperTypeOf($this->subtractedType)->yes()) {
return $int;
}
if ($int->isSuperTypeOf($this->subtractedType)->yes()) {
return $float;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What if both float and int are subtracted?

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, can someone explain to me how this works in general? if something is e.g. not a float it could be e.g. an object still and objects can't be incremented?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if both float and int are subtracted?

yeah, we could return a NeverType then.

sorry, can someone explain to me how this works in general? if something is e.g. not a float it could be e.g. an object still and objects can't be incremented?

the incrementing test-case is kind of synthetic.. I "reverse engineered" it from looking at toNumber() uses in the codebase:

if ($node instanceof Node\Expr\UnaryPlus) {
return $this->getType($node->expr)->toNumber();
}

decting invalid cases like $object++ is the job of a rule, not the narrowing done here I think

Copy link
Member

Choose a reason for hiding this comment

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

Even if float is subtracted, the result can still be a float... Because MixedType without FloatType can still be numeric-string for example... https://3v4l.org/StqSP

if (!is_int($mixed)) {
assertType('mixed~float|int', $mixed);
$mixed++;
assertType('*NEVER*', $mixed);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could still be a string though? https://3v4l.org/D4BHP

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah,.. I think the src/ changes actually makes sense .. but my test is to synthetic and your example shows that we cannot return a Never type for toNumber

@staabm staabm marked this pull request as draft November 2, 2022 18:24
@staabm staabm force-pushed the mixed-to-number branch 2 times, most recently from fbd9f6d to 0450d0f Compare November 2, 2022 18:44
@staabm staabm marked this pull request as ready for review November 2, 2022 18:46
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the mixed-to-number branch 3 times, most recently from 03f0a1b to 52539e7 Compare November 5, 2022 07:32
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Please return ErrorType if the subtracted type makes sure it can never be a number. Thanks.

@ondrejmirtes
Copy link
Member

Oh actually, other types can be cast to number as well 🤦

@staabm
Copy link
Contributor Author

staabm commented Nov 5, 2022

thank you guys for all the examples and ideas.

I think we / I learnt from it, that we can't take subtractable into account in MixedType->toNumber, as there are a lot of situations which will lead to int, float or int|float even when the summand types are not even numbers.

I put all the examples into a unit test and fixed a edge case with mixed~array by that.

the PR no longer does what its title suggested, but at least our time discussing it is not lost, but manifested in more testcoverage.

thanks again.

@ondrejmirtes
Copy link
Member

Yeah, at least it was still productive :) Thank you.

@ondrejmirtes ondrejmirtes merged commit 47ba52b into phpstan:1.9.x Nov 5, 2022
@staabm staabm deleted the mixed-to-number branch November 5, 2022 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants