Skip to content

Narrow array on count() with positive-int #3389

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 1 commit into from
Sep 5, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 2, 2024

With this PR we also narrow in case of unbound integer ranges

separated from #3373

Comment on lines -47 to -52
assertType('non-empty-array', $arr);
assertType('array', $arr);
}
if (count($arr) === $maybeZero) {
assertType('array', $arr);
} else {
assertType('non-empty-array', $arr);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I did a mistake here in previous PR, as the array can still be empty in the else branch

https://3v4l.org/409Jp

@@ -1046,7 +1047,7 @@ private function turnListIntoConstantArray(FuncCall $countFuncCall, Type $type,
$offsetType = new ConstantIntegerType($i);
$valueTypesBuilder->setOffsetValueType($offsetType, $type->getOffsetValueType($offsetType), true);
}
} else {
} elseif ($type->isConstantArray()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

compensated the now passed in maybe unbound IntegerRangeTypes

@staabm staabm marked this pull request as ready for review September 2, 2024 18:35
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

&& count($unwrappedLeftExpr->getArgs()) >= 1
&& $unwrappedLeftExpr->name instanceof Name
&& in_array(strtolower((string) $unwrappedLeftExpr->name), ['count', 'sizeof'], true)
&& $rightType->isInteger()->yes()
Copy link
Contributor Author

@staabm staabm Sep 2, 2024

Choose a reason for hiding this comment

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

uses now a proper Type method instead of && $constantType instanceof ConstantIntegerType

@staabm staabm force-pushed the count-const branch 2 times, most recently from a47ea23 to 5b20101 Compare September 3, 2024 07:33
@ondrejmirtes ondrejmirtes merged commit 5379e31 into phpstan:1.12.x Sep 5, 2024
487 of 499 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the count-const branch September 5, 2024 16:13
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.

None yet

3 participants