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

Narrow string on strlen() == and === comparison with integer-range #3342

Merged
merged 1 commit into from
Aug 29, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 22, 2024

closes phpstan/phpstan#11548


after implementing I realized this also makes count($arr) == integer-range narrow to non-empty-array

@@ -2172,6 +2172,29 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

if ($rightType instanceof IntegerRangeType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

inspired by the previous loop with constant-strings, I have added a similar case for integer ranges

Comment on lines -1093 to -1096
if ($constantType->getValue() === null) {
return $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looked like dead code

@staabm staabm marked this pull request as ready for review August 26, 2024 07:34
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm marked this pull request as draft August 26, 2024 08:17
@staabm staabm marked this pull request as ready for review August 26, 2024 09:25
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -1102,6 +1104,10 @@ private function specifyTypesForConstantBinaryExpression(
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
&& $constantType instanceof ConstantIntegerType
Copy link
Contributor Author

Choose a reason for hiding this comment

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

in a followup PR I would factor out the 2 $constantType instanceof ConstantIntegerType if-blocks of this method into the parent caller and do a proper integer range check instead, which will fix phpstan/phpstan#11558

I don't do it now to keep this PR simpler and the next one will also be easier to review

@staabm staabm marked this pull request as ready for review August 27, 2024 08:28
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

simplify

Update TypeSpecifier.php

Update TypeSpecifier.php

remove seemingly dead code

Update strlen-int-range.php

more tests

more tests

test count on int-range

more tests

more assertions

fix negative context

cs

simplify
@staabm staabm changed the base branch from 1.11.x to 1.12.x August 27, 2024 14:10
@ondrejmirtes ondrejmirtes merged commit 562b730 into phpstan:1.12.x Aug 29, 2024
492 of 503 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the bug11548 branch August 29, 2024 21:45
This pull request was closed.
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.

narrow string on strlen() == and === comparison with integer-range
3 participants