-
Notifications
You must be signed in to change notification settings - Fork 542
Narrow string on *strlen() with positive-int #3407
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1124,39 +1124,6 @@ private function specifyTypesForConstantBinaryExpression( | |
| )); | ||
| } | ||
|
|
||
| if ( | ||
| !$context->null() | ||
| && $exprNode instanceof FuncCall | ||
| && count($exprNode->getArgs()) === 1 | ||
| && $exprNode->name instanceof Name | ||
| && in_array(strtolower((string) $exprNode->name), ['strlen', 'mb_strlen'], true) | ||
| && $constantType instanceof ConstantIntegerType | ||
| ) { | ||
| if ($constantType->getValue() < 0) { | ||
| return $this->create($exprNode->getArgs()[0]->value, new NeverType(), $context, false, $scope, $rootExpr); | ||
| } | ||
|
|
||
| if ($context->truthy() || $constantType->getValue() === 0) { | ||
| $newContext = $context; | ||
| if ($constantType->getValue() === 0) { | ||
| $newContext = $newContext->negate(); | ||
| } | ||
| $argType = $scope->getType($exprNode->getArgs()[0]->value); | ||
| if ($argType->isString()->yes()) { | ||
| $funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr); | ||
|
|
||
| $accessory = new AccessoryNonEmptyStringType(); | ||
| if ($constantType->getValue() >= 2) { | ||
| $accessory = new AccessoryNonFalsyStringType(); | ||
| } | ||
| $valueTypes = $this->create($exprNode->getArgs()[0]->value, $accessory, $newContext, false, $scope, $rootExpr); | ||
|
|
||
| return $funcTypes->unionWith($valueTypes); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -2140,6 +2107,42 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty | |
| } | ||
| } | ||
|
|
||
| if ( | ||
| !$context->null() | ||
| && $unwrappedLeftExpr instanceof FuncCall | ||
| && count($unwrappedLeftExpr->getArgs()) === 1 | ||
| && $unwrappedLeftExpr->name instanceof Name | ||
| && in_array(strtolower((string) $unwrappedLeftExpr->name), ['strlen', 'mb_strlen'], true) | ||
| && $rightType->isInteger()->yes() | ||
| ) { | ||
| if (IntegerRangeType::fromInterval(null, -1)->isSuperTypeOf($rightType)->yes()) { | ||
| return $this->create($unwrappedLeftExpr->getArgs()[0]->value, new NeverType(), $context, false, $scope, $rootExpr); | ||
| } | ||
|
|
||
| $isZero = (new ConstantIntegerType(0))->isSuperTypeOf($rightType); | ||
| if ($isZero->yes()) { | ||
| $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, false, $scope, $rootExpr); | ||
| return $funcTypes->unionWith( | ||
| $this->create($unwrappedLeftExpr->getArgs()[0]->value, new ConstantStringType(''), $context, false, $scope, $rootExpr), | ||
| ); | ||
| } | ||
|
|
||
| if ($context->truthy() && IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($rightType)->yes()) { | ||
| $argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value); | ||
| if ($argType->isString()->yes()) { | ||
| $funcTypes = $this->create($unwrappedLeftExpr, $rightType, $context, false, $scope, $rootExpr); | ||
|
|
||
| $accessory = new AccessoryNonEmptyStringType(); | ||
| if (IntegerRangeType::fromInterval(2, null)->isSuperTypeOf($rightType)->yes()) { | ||
| $accessory = new AccessoryNonFalsyStringType(); | ||
| } | ||
| $valueTypes = $this->create($unwrappedLeftExpr->getArgs()[0]->value, $accessory, $context, false, $scope, $rootExpr); | ||
|
|
||
| return $funcTypes->unionWith($valueTypes); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| $context->true() | ||
| && $unwrappedLeftExpr instanceof FuncCall | ||
|
|
@@ -2209,14 +2212,11 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty | |
| } | ||
| } | ||
|
|
||
| if ($rightType->isInteger()->yes() || $rightType->isString()->yes()) { | ||
| if ($rightType->isString()->yes()) { | ||
|
||
| $types = null; | ||
| foreach ($rightType->getFiniteTypes() as $finiteType) { | ||
| if ($finiteType->isString()->yes()) { | ||
| $specifiedType = $this->specifyTypesForConstantStringBinaryExpression($unwrappedLeftExpr, $finiteType, $context, $scope, $rootExpr); | ||
| } else { | ||
| $specifiedType = $this->specifyTypesForConstantBinaryExpression($unwrappedLeftExpr, $finiteType, $context, $scope, $rootExpr); | ||
| } | ||
| foreach ($rightType->getConstantStrings() as $constantString) { | ||
| $specifiedType = $this->specifyTypesForConstantStringBinaryExpression($unwrappedLeftExpr, $constantString, $context, $scope, $rootExpr); | ||
|
|
||
| if ($specifiedType === null) { | ||
| continue; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1283,7 +1283,7 @@ public function dataCondition(): iterable | |
| ), | ||
| ), | ||
| [ | ||
| '$foo' => 'non-empty-string', | ||
| '$foo' => "string & ~''", | ||
|
||
| 'strlen($foo)' => '~0', | ||
| ], | ||
| [ | ||
|
|
||
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.
instead of comparison with
ConstantIntegerTypewe now supportIntegerRangeType