Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Oct 3, 2025

if the for loop is in the shape of for($i = 0; $i < count($items); $i++) { we can set $items[$i] as existing expression type.

closes phpstan/phpstan#12807

@staabm
Copy link
Contributor Author

staabm commented Oct 3, 2025

we could make this work for more involved loops, but I think this will work as a first step

--

other examples

  • for ($i = count($items) - 1; $i >= 0; --$i) {
  • for ($i = count($items) - 1; $i >= 0; $i--) {
  • for ($i = count($items) - 1; $i > -1; $i--) {
  • for ($i = count($disk)-1, $f = 0; $i >= 0; $i--) {
  • for ($i = count($bytes)-2; $i>=0; $i--)
  • for ($i = 0; $i < count($items); $i+=4) {

@staabm staabm marked this pull request as ready for review October 3, 2025 16:23
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

&& $stmt->init[0] instanceof Assign
&& $stmt->init[0]->var instanceof Variable
&& $stmt->init[0]->expr instanceof Node\Scalar\Int_
&& $stmt->init[0]->expr->value === 0
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 we could do >=0

@ondrejmirtes ondrejmirtes merged commit a5aa2ce into phpstan:2.1.x Oct 3, 2025
546 of 551 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the bug12807 branch October 3, 2025 19:47
@staabm
Copy link
Contributor Author

staabm commented Oct 3, 2025

any opinions on the other given examples? do you think this can work and should be implemented?

@ondrejmirtes
Copy link
Member

Instead of writing many many conditions, I'd like if this could be expressed more by the type system. That would cover all situations. But I'm not sure yet how it could look.

@staabm
Copy link
Contributor Author

staabm commented Oct 4, 2025

ok thanks. no priority on it.

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.

list type lost in for loop
3 participants