Skip to content

Fix item-type in list to constant-array conversion with count() #3309

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

Merged
merged 11 commits into from
Aug 19, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Aug 9, 2024

before this PR we were loosing precision in this process as item-types were kind of generalized before.

I guess the original implementation was done before Type->getOffsetValueType() was a thing.

@staabm staabm force-pushed the list-to-const-array branch from 1e63d51 to afe2113 Compare August 9, 2024 09:43
@staabm staabm marked this pull request as ready for review August 9, 2024 09:55
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the list-to-const-array branch 4 times, most recently from 1133d1a to 7574911 Compare August 12, 2024 09:45
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
for ($i = 0; $i < $constantType->getValue(); $i++) {
$offsetType = new ConstantIntegerType($i);
$valueTypesBuilder->setOffsetValueType($offsetType, $innerType->getOffsetValueType($offsetType));
Copy link
Member

Choose a reason for hiding this comment

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

What about doing intersect with HasOffsetType instead of the for loop?

Copy link
Contributor Author

@staabm staabm Aug 13, 2024

Choose a reason for hiding this comment

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

I tried using HasOffsetValueType but it still requires the loop

-                                               $valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
+                                               $hasOffsetTypes = [];
                                                for ($i = 0; $i < $constantType->getValue(); $i++) {
-                                                       $offsetType = new ConstantIntegerType($i);
-                                                       $valueTypesBuilder->setOffsetValueType($offsetType, $argType->getOffsetValueType($offsetType));
+                                                       $hasOffsetTypes[] = new HasOffsetValueType(new ConstantIntegerType($i), $argType->getOffsetValueType(new ConstantIntegerType(
$i)));
                                                }
-                                               $valueTypes = $this->create($exprNode->getArgs()[0]->value, $valueTypesBuilder->getArray(), $context, false, $scope, $rootExpr);
+                                               $valueTypes = $this->create($exprNode->getArgs()[0]->value, TypeCombinator::intersect($argType, ...$hasOffsetTypes), $context, false,
 $scope, $rootExpr);

and it leads to less precise types, I think (at least less readable)

--- Expected
+++ Actual
@@ @@
-'array{ListCount\A, ListCount\A, ListCount\A}'
+'list<ListCount\A>&hasOffsetValue(0, ListCount\A)&hasOffsetValue(1, ListCount\A)&hasOffsetValue(2, ListCount\A)'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to drop the loop we could maybe use new HasOffsetValueType(IntegerRangeType::fromInterval(0, $constantType->getValue(), $offsetValueType) but this doesn't work because $offsetValueType is different per offset

@staabm staabm marked this pull request as draft August 13, 2024 09:05
@staabm staabm marked this pull request as ready for review August 13, 2024 09:10
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the list-to-const-array branch from 7574911 to 18d20a8 Compare August 13, 2024 09:11
@ondrejmirtes
Copy link
Member

Conflict :)

@staabm staabm force-pushed the list-to-const-array branch from 18d20a8 to 95d749e Compare August 13, 2024 19:57
@staabm staabm marked this pull request as draft August 13, 2024 20:02
@staabm staabm marked this pull request as ready for review August 13, 2024 20:49
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

if (
$isNormalCount->yes()
&& $type->isList()->yes()
&& $sizeType instanceof ConstantIntegerType
Copy link
Contributor Author

@staabm staabm Aug 18, 2024

Choose a reason for hiding this comment

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

Note to future me: Misses int-range support?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here we go: #3335

@ondrejmirtes ondrejmirtes merged commit f618124 into phpstan:1.11.x Aug 19, 2024
453 of 463 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

3 participants