-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#14215: Nonexistent offset detection on lists with count() -1
#5116
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
Open
phpstan-bot
wants to merge
1
commit into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-xaz8gw4
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -266,36 +266,56 @@ public function specifyTypesInCondition( | |
| $leftType = $scope->getType($expr->left); | ||
| $result = (new SpecifiedTypes([], []))->setRootExpr($expr); | ||
|
|
||
| $countFuncCall = null; | ||
| $subtraction = 0; | ||
|
|
||
| if ( | ||
| !$context->null() | ||
| && $expr->right instanceof FuncCall | ||
| $expr->right instanceof FuncCall | ||
| && $expr->right->name instanceof Name | ||
| && in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true) | ||
| && count($expr->right->getArgs()) >= 1 | ||
| ) { | ||
| $countFuncCall = $expr->right; | ||
| } elseif ( | ||
| $expr->right instanceof Node\Expr\BinaryOp\Minus | ||
| && $expr->right->left instanceof FuncCall | ||
| && $expr->right->left->name instanceof Name | ||
| && in_array(strtolower((string) $expr->right->left->name), ['count', 'sizeof'], true) | ||
| && count($expr->right->left->getArgs()) >= 1 | ||
| && $expr->right->right instanceof Node\Scalar\Int_ | ||
| && $expr->right->right->value >= 1 | ||
| ) { | ||
| $countFuncCall = $expr->right->left; | ||
| $subtraction = $expr->right->right->value; | ||
| } | ||
|
|
||
| if ( | ||
| !$context->null() | ||
| && $countFuncCall !== null | ||
| && $leftType->isInteger()->yes() | ||
| ) { | ||
| $argType = $scope->getType($expr->right->getArgs()[0]->value); | ||
| $argType = $scope->getType($countFuncCall->getArgs()[0]->value); | ||
|
|
||
| if ($leftType instanceof ConstantIntegerType) { | ||
| if ($orEqual) { | ||
| $sizeType = IntegerRangeType::createAllGreaterThanOrEqualTo($leftType->getValue()); | ||
| $sizeType = IntegerRangeType::createAllGreaterThanOrEqualTo($leftType->getValue() + $subtraction); | ||
| } else { | ||
| $sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue()); | ||
| $sizeType = IntegerRangeType::createAllGreaterThan($leftType->getValue() + $subtraction); | ||
| } | ||
| } elseif ($leftType instanceof IntegerRangeType) { | ||
| $sizeType = $leftType->shift($offset); | ||
| $sizeType = $leftType->shift($offset + $subtraction); | ||
| } else { | ||
| $sizeType = $leftType; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we add |
||
| } | ||
|
|
||
| $specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr); | ||
| $specifiedTypes = $this->specifyTypesForCountFuncCall($countFuncCall, $argType, $sizeType, $context, $scope, $expr); | ||
| if ($specifiedTypes !== null) { | ||
| $result = $result->unionWith($specifiedTypes); | ||
| } | ||
|
|
||
| if ( | ||
| $context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes()) | ||
| || ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes()) | ||
| $context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset - $subtraction)->isSuperTypeOf($leftType)->yes()) | ||
| || ($context->false() && (new ConstantIntegerType(1 - $offset - $subtraction))->isSuperTypeOf($leftType)->yes()) | ||
| ) { | ||
| if ($context->truthy() && $argType->isArray()->maybe()) { | ||
| $countables = []; | ||
|
|
@@ -318,7 +338,7 @@ public function specifyTypesInCondition( | |
| if (count($countables) > 0) { | ||
| $countableType = TypeCombinator::union(...$countables); | ||
|
|
||
| return $this->create($expr->right->getArgs()[0]->value, $countableType, $context, $scope)->setRootExpr($expr); | ||
| return $this->create($countFuncCall->getArgs()[0]->value, $countableType, $context, $scope)->setRootExpr($expr); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -329,21 +349,21 @@ public function specifyTypesInCondition( | |
| } | ||
|
|
||
| $result = $result->unionWith( | ||
| $this->create($expr->right->getArgs()[0]->value, $newType, $context, $scope)->setRootExpr($expr), | ||
| $this->create($countFuncCall->getArgs()[0]->value, $newType, $context, $scope)->setRootExpr($expr), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // infer $list[$index] after $index < count($list) | ||
| // infer $list[$index] after $index < count($list) or $index < count($list) - K | ||
| if ( | ||
| $context->true() | ||
| && !$orEqual | ||
| && (!$orEqual || $subtraction >= 1) | ||
| // constant offsets are handled via HasOffsetType/HasOffsetValueType | ||
| && !$leftType instanceof ConstantIntegerType | ||
| && $argType->isList()->yes() | ||
| && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes() | ||
| ) { | ||
| $arrayArg = $expr->right->getArgs()[0]->value; | ||
| $arrayArg = $countFuncCall->getArgs()[0]->value; | ||
| $dimFetch = new ArrayDimFetch($arrayArg, $expr->left); | ||
| $result = $result->unionWith( | ||
| $this->create($dimFetch, $argType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope)->setRootExpr($expr), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14215; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntLessThanCountMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index < count($array) - 1) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntLessThanOrEqualCountMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index <= count($array) - 1) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| */ | ||
| public function intLessThanOrEqualCountMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index <= count($array) - 1) { | ||
| return $array[$index]; // should error report, could be negative int | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param int<0, max> $index | ||
| */ | ||
| public function nonNegativeIntLessThanCountMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index < count($array) - 1) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param int<0, max> $index | ||
| */ | ||
| public function nonNegativeIntLessThanOrEqualCountMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index <= count($array) - 1) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntLessThanCountMinusTwo(array $array, int $index): int | ||
| { | ||
| if ($index < count($array) - 2) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntLessThanOrEqualCountMinusTwo(array $array, int $index): int | ||
| { | ||
| if ($index <= count($array) - 2) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntLessThanSizeofMinusOne(array $array, int $index): int | ||
| { | ||
| if ($index < sizeof($array) - 1) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param list<int> $array | ||
| * @param positive-int $index | ||
| */ | ||
| public function positiveIntGreaterThanCountMinusOneInversed(array $array, int $index): int | ||
| { | ||
| if (count($array) - 1 > $index) { | ||
| return $array[$index]; // should not report | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think the bot is trying to support thing like