Skip to content

Array built in a nested while loop becomes array{0?: ...} in a later for loop: "Offset might not exist" (regression in 2.3.x-dev) #15736

Closed
phpstan/phpstan-src
#7075

Description

@ondrejmirtes

Bug report

On 2.3.x-dev, an array filled in a while loop nested in another while loop is a general array<int<0, max>, ...> after the loops. Inside a later for loop, its type becomes array{0?: ...}, which reports Offset int<0, max> might not exist (level 7+):

40: Offset int<0, max> might not exist on array{0?: non-empty-array<int<0, max>, int>}.

With \PHPStan\dumpType($values) added to the snippet, the type is array<int<0, max>, non-empty-array<int<0, max>, int>> before the for loop and array{0?: non-empty-array<int<0, max>, int>} at the start of its body. The array isn't assigned in the for loop at all; the only connection is $col < $cols, where $cols is counted in the first inner while loop, which runs only on the first outer iteration.

All of these are needed to reproduce it: an assignment in the outer while condition (while ($row = fetchRow($rows))), the first inner loop being conditional, and $items being reassigned before the second inner loop.

Bisected in phpstan-src: the last good commit is 2a70999b2's parent, and the first bad one is phpstan/phpstan-src@2a70999 "Walk a while loop's exit condition instead of narrowing the body's scope by it" (phpstan/phpstan-src#7063). I checked both commits with bin/phpstan builds of phpstan-src at level 8. The PHPStan 2.3.x-dev PHAR from fef62b1/4ce4b19 doesn't report it, and the PHARs from ca825b0 onward do.

Real-world impact: openemr/openemr (944c214) in the integration tests, interface/forms/track_anything/history.php:324 and report.php:104, starting with PHAR ca825b0 in https://github.com/phpstan/phpstan/actions/runs/43581367799. The snippet was reduced from history.php.

Code snippet that reproduces the problem

https://phpstan.org/r/78c5397e-1349-41ca-8bac-ddc1a7419666

(The playground also shows a second error on line 40 about non-empty-array<int<0, max>, int>. That's its reportPossiblyNonexistentGeneralArrayOffset hint, not part of this report.)

Expected output

No error. $values should stay array<int<0, max>, non-empty-array<int<0, max>, int>> in the for loop, as before 2a70999b2.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. ondrejmirtes commented on Nov 15, 2026

    @ondrejmirtes
    MemberAuthor

    The same commit also changes a related type in openemr's interface/forms/track_anything/report.php:126: attr_js now gets array<int, array<int, float|int|string>> instead of array<int<0, max>, array<int, float|int|string>> (checked on phpstan-src 2a70999b2 and its parent). It's probably the same array losing its key range. I left it out of the openemr baseline in #15737 for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions