Skip to content

fix(arrays): a destructuring condition rules out the null a null value gives its variables - #14607

Merged
orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/10668-destructuring-condition-not-null
Nov 23, 2026
Merged

orklah merged 1 commit into
vimeo:masterfrom
jirkace:fix/10668-destructuring-condition-not-null

Conversation

@jirkace

@jirkace jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

A destructuring used as a condition (if ([$a, $b] = f())) keeps the null its variables only get when the value is null, which the condition rules out.

Before

<?php
/** @return list{int, int}|null */
function pair(int $c): ?array {
    return $c ? [$c, $c] : null;
}

if ([$a, $b] = pair(1)) {
    echo $a + $b; // PossiblyNullOperand (twice)
}

After

    echo $a + $b; // no issue

Fixes #10668

…e gives its variables vimeo#10668

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

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • When a destructuring is a condition (if, while, ...) and the value may be null or false (which gives each variable null, and is falsy), a variable whose item every array the value may be surely has, not as null, is asserted not null where the condition holds. Applies to positions and literal keys of shapes, for [...] and list(...).
  • Unchanged: a nullable item (list{?int, int}|null, new invalid guard test), an item that may be missing (optional key, generic array<...>), and values that may be anything else than shapes, null and false. Where the condition fails, the variables are only known to be possibly null, as before.
  • AssignmentAnalyzer::getDestructuredOffsetValue() (internal class) is now public, to share how an item's key is read.

Not run: the full test suite (CI runs it).

@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 749 95.4 $\color{gray}{\textsf{-0.3\%}}$ 25 907 53 460 682
✅ symfony 3 599 205.4 $\color{gray}{\textsf{+3.5\%}}$ 50 608 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-2}}$ 98 397 $\color{green}{\textsf{-6}}$ 1 528
✅ doctrine-orm 380 20.4 $\color{gray}{\textsf{-0.1\%}}$ 3 481 6 143 0
✅ psl 617 26.6 $\color{gray}{\textsf{+1.0\%}}$ 1 375 5 615 1
✅ phpunit 455 11.6 $\color{gray}{\textsf{-0.2\%}}$ 1 731 3 410 18
✅ php-parser 226 7.5 $\color{gray}{\textsf{+2.3\%}}$ 17 152 $\color{red}{\textsf{+1}}$ $\color{green}{\textsf{-3}}$ 21 239 $\color{green}{\textsf{-6}}$ 0
✅ tempest 895 20.6 $\color{gray}{\textsf{-1.8\%}}$ 5 047 10 528 252
✅ amphp-amp 45 1.5 $\color{gray}{\textsf{-1.4\%}}$ 29 49 0
✅ webmozarts-assert 273 4.8 $\color{gray}{\textsf{+0.8\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 155 434 2
✅ monolog 135 5.7 $\color{gray}{\textsf{-0.7\%}}$ 913 2 479 10
✅ drupal 4 842 238.9 $\color{gray}{\textsf{-0.1\%}}$ 97 591 169 293 583
Total 13 260 639.8 $\color{gray}{\textsf{+1.0\%}}$ 204 566 $\color{red}{\textsf{+1}}$ $\color{green}{\textsf{-5}}$ 373 767 $\color{green}{\textsf{-12}}$ 3 076
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 +20 % 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. Thresholds apply per project, not to the total.
  • 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.
✅ symfony: issues +0 −2

Memory 3 599 → 3 599 MB (<0.1 %) · Wall 198.4 → 205.4 s (+3.5 %)

Weighted 98 403 → 98 397 (−6, −0.01 %): L3 −2

Issue type added removed
PossiblyNullArgument 0 1
PossiblyNullReference 0 1

Removed (2)

  • src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php:130 PossiblyNullArgument (error): Argument 1 of PhpDocExtractor::getTypeFromDocBlock cannot be null, possibly null value provided
  • src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php:149 PossiblyNullReference (error): Cannot call method getTagsByName on possibly null value
✅ php-parser: issues +1 −3

Memory 226 → 226 MB (<0.1 %) · Wall 7.4 → 7.5 s (+2.3 %)

Weighted 21 245 → 21 239 (−6, −0.03 %): L3 −2

Issue type added removed
MixedArgumentTypeCoercion 1 1
PossiblyNullArgument 0 2

Changed in place (1)

@​@​ lib/PhpParser/NodeTraverser.php:264 @​@​
- MixedArgumentTypeCoercion: Argument 2 of array_splice expects int, but parent type array-key|null provided
- PossiblyNullArgument:      Argument 2 of array_splice cannot be null, possibly null value provided
+ MixedArgumentTypeCoercion: Argument 2 of array_splice expects int, but parent type array-key provided

Removed (1)

  • lib/PhpParser/NodeTraverser.php:264 PossiblyNullArgument (error): Argument 4 of array_splice cannot be null, possibly null value provided

head 8e4b40a · base 11ded6c (merge-base) · workflow run

@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 777fafa 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.

Array destructuring doesn't eliminate null from elements if the right hand is confirmed to be non-null

2 participants