-
Notifications
You must be signed in to change notification settings - Fork 569
Pre-compute count-specific conditional expressions to narrow list types when count() is stored in a variable
#5461
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
Closed
phpstan-bot
wants to merge
3
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-7ltpyvc
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
83af88c
Pre-compute count-specific conditional expressions to narrow list typ…
phpstan-bot 5b65941
Generalize integer conditional pre-computation to all FuncCall expres…
phpstan-bot 74353c3
Remember FuncCall expression on variable assignment instead of pre-co…
phpstan-bot 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
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,114 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14464Analogous; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * sizeof() alias | ||
| * @param list<int> $items | ||
| */ | ||
| function testSizeof(array $items): void { | ||
| $count = sizeof($items); | ||
| if ($count === 3) { | ||
| assertType('array{int, int, int}', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Inline count still works | ||
| * @param list<int> $items | ||
| */ | ||
| function testInlineCount(array $items): void { | ||
| if (count($items) === 3) { | ||
| assertType('array{int, int, int}', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * explode() result | ||
| */ | ||
| function testExplode(string $input): void { | ||
| $parts = explode(',', $input); | ||
| $count = count($parts); | ||
| if ($count === 3) { | ||
| assertType('array{string, string, string}', $parts); | ||
| } elseif ($count === 1) { | ||
| assertType('array{string}', $parts); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variable count >= N (range comparison) | ||
| * @param list<int> $items | ||
| */ | ||
| function testGreaterOrEqual(array $items): void { | ||
| $count = count($items); | ||
| if ($count >= 3) { | ||
| assertType('non-empty-list<int>', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Count value > 8 (beyond pre-computed limit) | ||
| * @param list<int> $items | ||
| */ | ||
| function testBeyondLimit(array $items): void { | ||
| $count = count($items); | ||
| if ($count === 10) { | ||
| assertType('non-empty-list<int>', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Count with mode argument - safe for list<int> since int values are not countable | ||
| * @param list<int> $items | ||
| */ | ||
| function testCountWithMode(array $items, int $mode): void { | ||
| $count = count($items, $mode); | ||
| if ($count === 3) { | ||
| assertType('array{int, int, int}', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variable strlen - generalized integer pre-computation also works for strlen | ||
| */ | ||
| function testStrlen(string $s): void { | ||
| $len = strlen($s); | ||
| if ($len === 3) { | ||
| assertType('non-falsy-string', $s); | ||
| } elseif ($len === 1) { | ||
| assertType('non-empty-string', $s); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variable count on non-empty-list | ||
| * @param non-empty-list<string> $items | ||
| */ | ||
| function testNonEmptyList(array $items): void { | ||
| $count = count($items); | ||
| if ($count === 2) { | ||
| assertType('array{string, string}', $items); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variable count with switch statement | ||
| * @param list<int> $items | ||
| */ | ||
| function testSwitch(array $items): void { | ||
| $count = count($items); | ||
| switch ($count) { | ||
| case 1: | ||
| assertType('array{int}', $items); | ||
| break; | ||
| case 2: | ||
| assertType('array{int, int}', $items); | ||
| break; | ||
| case 3: | ||
| assertType('array{int, int, int}', $items); | ||
| break; | ||
| } | ||
| } |
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,72 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14464; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** Variable count with == (loose comparison from the issue) */ | ||
| protected function columnOrAlias(string $columnName): void | ||
| { | ||
| $colParts = preg_split('/\s+/', $columnName, -1, \PREG_SPLIT_NO_EMPTY); | ||
| if ($colParts === false) { | ||
| throw new \RuntimeException('preg error'); | ||
| } | ||
| assertType('list<non-empty-string>', $colParts); | ||
| $numParts = count($colParts); | ||
|
|
||
| if ($numParts == 3) { | ||
| assertType('array{non-empty-string, non-empty-string, non-empty-string}', $colParts); | ||
| $this->columnName($colParts[0]); | ||
| $this->columnName($colParts[1]); | ||
| $this->columnName($colParts[2]); | ||
| } elseif ($numParts == 2) { | ||
| assertType('array{non-empty-string, non-empty-string}', $colParts); | ||
| $this->columnName($colParts[0]); | ||
| $this->columnName($colParts[1]); | ||
| } elseif ($numParts == 1) { | ||
| assertType('array{non-empty-string}', $colParts); | ||
| $this->columnName($colParts[0]); | ||
| } else { | ||
| throw new \LogicException('invalid'); | ||
| } | ||
| } | ||
|
|
||
| /** Variable count with === (strict comparison) */ | ||
| protected function strictComparison(string $input): void | ||
| { | ||
| $parts = preg_split('/,/', $input, -1, \PREG_SPLIT_NO_EMPTY); | ||
| if ($parts === false) { | ||
| throw new \RuntimeException('preg error'); | ||
| } | ||
| $count = count($parts); | ||
|
|
||
| if ($count === 3) { | ||
| assertType('array{non-empty-string, non-empty-string, non-empty-string}', $parts); | ||
| } elseif ($count === 1) { | ||
| assertType('array{non-empty-string}', $parts); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Variable count on a PHPDoc list type | ||
| * @param list<int> $items | ||
| */ | ||
| protected function phpdocList(array $items): void | ||
| { | ||
| $count = count($items); | ||
| if ($count === 3) { | ||
| assertType('array{int, int, int}', $items); | ||
| } elseif ($count === 5) { | ||
| assertType('array{int, int, int, int, int}', $items); | ||
| } else { | ||
| assertType('list<int>', $items); | ||
| } | ||
| } | ||
|
|
||
| public function columnName(string $columnName): string | ||
| { | ||
| return 'abc'; | ||
| } | ||
| } |
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.
Could we remember the expression which a variable was assigned from, in case its a pure function call?
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.
Done. Here's a summary of the changes made to address staabm's review:
What changed
Instead of pre-computing conditional expressions for integer values 1-8 at assignment time (the
INTEGER_CONDITIONAL_LIMITloop), the code now remembers the original FuncCall expression that was assigned to a variable and resolves it dynamically when needed.Architecture
ExpressionTypeHolder- Added an optionalassignedFromExprfield. Since ExpressionTypeHolder lives in theexpressionTypesarray which is already threaded through all scope operations, no changes to the MutatingScope constructor or the 37scopeFactory->create()calls were needed.AssignHandler- AfterassignVariable(), if the assigned expression is aFuncCall, setsassignedFromExpron the ExpressionTypeHolder. Removed the pre-computation loop andINTEGER_CONDITIONAL_LIMITconstant.TypeSpecifier::resolveIdentical()- When one side is a Variable with a remembered FuncCall, also resolves the comparison with the original FuncCall substituted in and unions the results.MutatingScope- AddedgetVariableAssignedFromExpr()method. ModifiedspecifyExpressionType(),filterBySpecifiedTypes(),processScopeForBody(), andgeneralizeVariableTypeHolders()to preserveassignedFromExprthrough type narrowing, branch merges, and loop body processing. The remembered expression is automatically cleared on variable reassignment (sinceinvalidateExpressionremoves the old holder).Benefits over pre-computation
count($arr) === 10now correctly producesarray{int, int, int, int, int, int, int, int, int, int}(previously fell back tonon-empty-list<int>)Verification