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

Binary operations on non-empty-string and string should error #1010

Merged
merged 12 commits into from Feb 10, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 10, 2022

@ondrejmirtes
Copy link
Member

The right solution here is to introduce Type::isString() and use that. For example your code will continue to fail with literal-string.

@staabm
Copy link
Contributor Author

staabm commented Feb 10, 2022

The right solution here is to introduce Type::isString() and use that

hmm do you mean adding this method on the Type interface and implement it in all types?
if so, this is a BC break, isn't it?

@ondrejmirtes
Copy link
Member

It's not a BC break, implementing custom Type isn't covered by BC promise.

@staabm
Copy link
Contributor Author

staabm commented Feb 10, 2022

I see. I got the impression because the Type-interface has a @api annotation

grafik

@staabm staabm marked this pull request as ready for review February 10, 2022 15:01
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.

Otherwise 👍

@@ -1419,6 +1424,12 @@ private function resolveType(Expr $node): Type

$resultType = TypeCombinator::union($leftNumberType, $rightNumberType);
if ($node instanceof Expr\AssignOp\Div || $node instanceof Expr\BinaryOp\Div) {
// division on strings is not allowed. its allowed on numeric strings though.
if ($leftType->isString()->yes() && !$leftType->isNumericString()->yes() && !$leftType->isLiteralString()->yes()
|| $rightType->isString()->yes() && !$rightType->isNumericString()->yes() && !$rightType->isLiteralString()->yes()) {
Copy link
Member

Choose a reason for hiding this comment

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

  1. || vs. && operator priority needs to be disambiguated with parentheses
  2. I don't know why literal-string is mentioned in this condition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. fixed
  2. I mixed up my mental model in "123" would be a literal-string, but it is also a numeric-string, so the additional check is not necessary. fixed a bug so we don't accept literal-string for binary operations

@@ -1400,6 +1400,11 @@ private function resolveType(Expr $node): Type
return new ErrorType();
}

if ($leftType->isNonEmptyString()->yes() && !$leftType->isNumericString()->yes() ||
Copy link
Member

Choose a reason for hiding this comment

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

  1. || vs. && operator priority needs to be disambiguated with parentheses
  2. Why isNonEmptyString and not isString?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. fixed
  2. I used non-empty-string because the initial bug report was about non-emptry-string problems, while it already worked for regular string.
    I am now using isString instead of isNonEmptyString in the first block, which renders this second block of changes unnecessary. I removed it.

initially I had a different assumption what this method would provide
@ondrejmirtes ondrejmirtes merged commit 4f72fdc into phpstan:master Feb 10, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the bug6624 branch February 10, 2022 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants