-
Notifications
You must be signed in to change notification settings - Fork 506
Narrow string on strlen() == and === comparison with integer-range #3342
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
Conversation
src/Analyser/TypeSpecifier.php
Outdated
@@ -2172,6 +2172,29 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty | |||
} | |||
} | |||
|
|||
if ($rightType instanceof IntegerRangeType) { |
There was a problem hiding this comment.
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
if ($constantType->getValue() === null) { | ||
return $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looked like dead code
This pull request has been marked as ready for review. |
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 |
There was a problem hiding this comment.
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
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
Thank you! |
closes phpstan/phpstan#11548
after implementing I realized this also makes
count($arr) == integer-range
narrow tonon-empty-array