Skip to content

support subtracted-mixed in toNumber()#1949

Merged
ondrejmirtes merged 2 commits intophpstan:1.9.xfrom
staabm:mixed-to-number
Nov 5, 2022
Merged

support subtracted-mixed in toNumber()#1949
ondrejmirtes merged 2 commits intophpstan:1.9.xfrom
staabm:mixed-to-number

Conversation

@staabm
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

Comment on lines 410 to 415
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

@staabm
Copy link
Copy Markdown
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
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

5 participants