Skip to content

Do not project narrowings of expressions reading the reassigned variable into conditional expressions guarded by it - #7090

Closed
phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-c1mrpk1
Closed

phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-c1mrpk1

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

Analysis never finished on $s = strlen($s ?: "") ?: 0; (and on preg_split(..., $x ?: '', ...) ?: [] assigned back to $x). This was a regression in 2.3.6. When a variable is assigned, AssignHandler turns narrowings of parts of the assigned expression into conditional expressions guarded by that variable. It also did this for parts that read the variable itself, like strlen($s ?: ""). Those parts describe the variable's old value, so the guards were wrong. They led to infinite on-demand re-processing. These parts are now skipped.

Changes

  • src/Analyser/ExprHandler/AssignHandler.php: isExprSafeToProjectThroughVariable() returns false for any expression that contains a read of the assigned variable (NodeFinder::findFirst() for a Variable with that name).
  • turbo-ext/src/AssignHandler.cpp: the same check in the native mirror, through the shared pt_find_first_recursive() walker. The differential smoke test prints ALL OK, and analysis output is identical with and without the extension.
  • Analogous cases fixed by the same change: $f = $f->p !== null ? $f->next : null; and $f = $f->p === null ? null : $f->next; used to narrow $f->p of the new $f to int after if ($f !== null).
  • Probed and already correct: match assigned back into a variable it reads (its arm scopes go through assignVariable(), which invalidates them), $f = $f->next ?? null, and $a = isset($a['x']) ? ... : ....

Root cause

Before the assignment, the narrowings of the assigned expression's parts are recorded as conditional holders like "if $s is int<1, max> then strlen($s ?: "") is int<1, max>". assignVariable() drops remembered expressions containing $s, but these holders are added afterwards, so they survived even though they talk about the previous $s.

In the reported case, ParameterOutExecutionEndTypeRule asked for the type of the assignment on the execution-end scope. Because the variable state differs there, the assignment was processed on demand. The inner $s ?: "" ternary narrowed the new $s, so the stale holders fired, and strlen($s ?: "") got different types in the two branches. The merge then asked MutatingScope::findUninformativeMergedCalls() for the call's own type. Since the call was still being processed, that triggered another on-demand walk of the same call and ternary, recursing without end. With the stale holders gone, the branches no longer differ and the merge doesn't re-enter.

Test

tests/PHPStan/Analyser/nsrt/bug-15754.php contains:

  • the reported snippet
  • the preg_split() variant from the Rector report
  • type assertions on the reassigned variable
  • the two $f = $f->p ... $f->next cases that inferred int instead of int|null

Without the fix, the test hangs (killed by timeout). With the fix, it passes both with and without the turbo extension.

Fixes phpstan/phpstan#15754

…ble into conditional expressions guarded by it

- AssignHandler::isExprSafeToProjectThroughVariable() now rejects any
  expression that reads the variable being assigned. For
  `$s = strlen($s ?: "") ?: 0` the narrowings of `$s ?: ""` and
  `strlen($s ?: "")` were recorded as "if $s=int<1, max> then
  strlen($s ?: "") is int<1, max>". Those expressions describe the old $s,
  so the holders were stale. They were added after assignVariable() had
  already invalidated the expressions, so they survived.
- The stale holders caused the hang. A rule priced the assignment on the
  execution-end scope, which processed it on demand. The inner ternary's
  branches fired the holders, so `strlen($s ?: "")` differed between
  them. MutatingScope::findUninformativeMergedCalls() then priced the
  call again on demand, which walked the same ternary again, with no end.
- The same stale projection also gave wrong types without a hang:
  `$f = $f->p !== null ? $f->next : null; if ($f !== null) { $f->p }`
  inferred `int` instead of `int|null`, as did the `=== null` form.
- Ported the same check to turbo-ext/src/AssignHandler.cpp, using the
  shared findFirst walker.
- Probed `match`-assigned values, `??`, and `isset($a['x'])`-based ternaries
  that reassign the read variable. Those were already correct (match arm
  scopes go through assignVariable() and its invalidation).
@staabm

staabm commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

I like #7088 more

@staabm staabm closed this Nov 16, 2026
@staabm
staabm deleted the create-pull-request/patch-c1mrpk1 branch November 16, 2026 13:15
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.

Analysis never finishes on $s = strlen($s ?: "") ?: 0; (regression in 2.3.6)

2 participants