Skip to content

fix(loops): a variable unset before a continue or break is possibly undefined after the loop - #14509

Merged
orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14486-unset-at-continue
Nov 23, 2026
Merged

orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14486-unset-at-continue

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

A variable unset in a loop before a continue or a break, or unset conditionally in the body, isn't reported as possibly undefined after the loop.

Before

<?php
/** @param non-empty-list<int> $items */
function f(array $items): void {
    $x = 0;
    foreach ($items as $i) {
        if ($i) {
            unset($x);
            continue;
        }
        $x = 5;
    }
    echo $x; // no issue
}

After

    echo $x; // PossiblyUndefinedVariable

Fixes #14486

…ndefined after the loop vimeo#14486

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

PossiblyUndefinedVariable is now reported after a loop (foreach, while, do, for) for a variable defined before it that the loop may leave unset:

  • unset before a continue (also continue 2 of an outer loop, and continue in a switch), unless the loop condition or a for post-expression evaluated after the continue assigns it;
  • unset before a break;
  • unset conditionally in the body (if ($i) { unset($x); }), which a loop that may not be entered also lost: the merge after the loop compared the types without their possibly_undefined flag.

Unchanged: a variable unset and assigned again before the jump, a jump through a finally (the finally may assign it), properties and array offsets (only plain variables). The side note of the issue (0 kept in the type after a loop over a non-empty list) is not addressed here.

Verified: the 5 new invalid cases (ForeachTest ×4, WhileTest, DoTest: varUnset…) fail on master and pass here, the 4 new valid guard cases pass on both; existing loop cases with unset + break/continue (whileTrueVariableUnsetAtABreakIsPossiblyUndefined, whileTrueVariableUnsetAtEveryBreakIsReported, doWhileVarUnsetByFinallyAfterContinue, whileTrueVariableDefinedBeforeTheLoopIsWhatTheBreaksLeave, 3 foreachByReference…) still pass. Corpora (errorLevel 1): laravel 27864, symfony 11700, drupal 33457 lines, identical before and after. Full self-analysis: no new issue (only 2 MixedArgument from my local revolt/event-loop v1.0.9, unrelated).

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 75.5 $\color{gray}{\textsf{-1.0\%}}$ 25 792 53 100 743 0
✅ symfony 3 619 281.4 $\color{gray}{\textsf{-0.1\%}}$ 51 767 100 629 1 582 0
✅ doctrine-orm 381 17.7 $\color{gray}{\textsf{-0.6\%}}$ 3 459 6 218 0 0
✅ psl 618 20.9 1 387 5 600 1 0
✅ phpunit 456 20.9 $\color{gray}{\textsf{+0.3\%}}$ 1 735 3 418 18 0
✅ php-parser 226 14.5 $\color{gray}{\textsf{+2.0\%}}$ 17 116 21 187 0 0
✅ tempest 898 38.6 $\color{gray}{\textsf{-0.3\%}}$ 5 094 10 556 275 0
✅ amphp-amp 48 1.5 $\color{gray}{\textsf{+0.7\%}}$ 29 49 0 0
✅ webmozarts-assert 273 4.4 $\color{gray}{\textsf{+1.4\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 1.8 $\color{gray}{\textsf{+9.4\%}}$ 155 433 2 0
✅ monolog 136 5.7 $\color{gray}{\textsf{-0.2\%}}$ 918 2 491 11 0
✅ drupal 4 847 236.3 $\color{gray}{\textsf{+0.9\%}}$ 97 949 169 915 575 0
✅ wordpress 2 061 186.0 $\color{gray}{\textsf{-0.1\%}}$ 143 633 678 503 505 1
Total 15 381 905.2 $\color{gray}{\textsf{+0.1\%}}$ 349 609 1 054 813 3 712 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head 985aa73 · base b5ea40d (merge-base) · workflow run

…s is possibly undefined after the loop vimeo#14486

The variable was made possibly undefined only when it was missing at every
`break` (or `continue`); unset before one and set at another, it is in the
merged state as possibly undefined, which was not applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Review finding of the triage report (head e3ec93e), confirmed and fixed in f0ed13b: with if ($a) { unset($x); break; } if ($b) { break; }, $x is in the merged state at the breaks as possibly undefined, not missing. The check only looked for a missing entry, so nothing was reported after the loop. The same held for continue. It now also applies a possibly undefined entry.

  • New tests: ForeachTest::testInvalidCode@varUnsetBeforeOneOfSeveralBreaksIsPossiblyUndefinedAfterTheLoop and @varUnsetBeforeOneOfSeveralContinuesIsPossiblyUndefinedAfterTheLoop. Both fail on the previous head with no issue.
  • The 11 cases of this PR in ForeachTest, DoTest, ForTest and WhileTest still pass. That includes the guards for a variable redefined before the jump, assigned by the condition or post-expression, or assigned in a finally.
  • Self-analysis shows only the two local PoolEventLoopDriverTest MixedArgument errors. phpcs is clean. Not run: the full suite (CI runs it).

# Conflicts:
#	tests/Loop/ForeachTest.php
# Conflicts:
#	tests/Loop/WhileTest.php
# Conflicts:
#	tests/Loop/ForTest.php
#	tests/Loop/WhileTest.php
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026
@orklah

orklah commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit 4431847 into vimeo:master Nov 23, 2026
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable unset before continue in foreach is not possibly undefined after the loop

2 participants