Skip to content

fix(coalesce): ($a ?? null) !== null asserts isset($a), ?? of a set non-null variable is the variable - #14517

Merged
orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/14251-coalesce-null-narrowing
Nov 24, 2026
Merged

orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/14251-coalesce-null-narrowing

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

($a['k'] ?? null) !== null does not narrow $a['k'], so repeating the same ?? expression in the branch is still possibly null.

Before

<?php
final class Item {
    public function getName(): string {
        return "x";
    }
}

/** @param array<string, Item> $data */
function repeated(array $data): string {
    if (($data["item"] ?? null) !== null) {
        return ($data["item"] ?? null)->getName(); // PossiblyNullReference
    }
    return "";
}

After

        return ($data["item"] ?? null)->getName(); // no issue

Fixes #14251

…on-null variable is the variable vimeo#14251

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

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • ($a ?? null) === null / !== null (strict, right side the null literal) now asserts !isset($a) / isset($a) on the left operand, which is exactly when they hold. Before, only the whole expression was narrowed, so the offset or variable itself stayed possibly null/undefined (PossiblyNullReference on a repeated ($data["item"] ?? null)->getName(), PossiblyUndefinedStringArrayOffset on $data["item"] with ensureArrayStringOffsetsExist).
  • $a ?? $b where $a is a local variable or an offset of one that is known to be set and not null is now the type of $a only (the right side is never evaluated); before it always added the type of $b. Properties are left out, as a typed property may be uninitialized whatever its type says ($this->p ??= new P() is unchanged).
  • Unchanged: loose == null comparisons, ?? with a possibly unset/null left side, the RedundantCondition/TypeDoesNotContainNull already reported for a ?? on a never-null variable.

Verified: IssetTest::testValidCode@coalesceToNullComparedWithNullNarrowsTheCoalescedOffset and @coalesceOfASetNonNullVariableIsTheVariable fail on master and pass here. Corpora (errorLevel 1): symfony −1 (PossiblyUndefinedStringArrayOffset in Serializer/Mapping/AttributeMetadata.php:305, null !== ($data['serializedPath'] ?? null) ? ['*' => $data['serializedPath']] : [], the case of the issue), laravel one message change at the same location (MixedReturnStatement "Could not infer a return type" → "Possibly-mixed return value", Collections/functions.php:20), drupal identical. A first version that also took properties changed ~30 laravel/symfony results (lazy ??= of typed properties): that is why properties are excluded. Full self-analysis: no new issue (only 2 MixedArgument from my local revolt/event-loop v1.0.9, unrelated; run before the property exclusion, the changed files re-checked after it).

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 91.2 $\color{gray}{\textsf{-0.3\%}}$ 25 787 53 067 743 0
✅ symfony 3 619 187.9 $\color{gray}{\textsf{+0.7\%}}$ 51 737 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-2}}$ 100 450 $\color{green}{\textsf{-5}}$ 1 582 0
✅ doctrine-orm 381 23.9 $\color{gray}{\textsf{+0.3\%}}$ 3 453 6 194 0 0
✅ psl 618 27.8 $\color{gray}{\textsf{+0.7\%}}$ 1 387 5 600 1 0
✅ phpunit 456 11.4 $\color{gray}{\textsf{+2.0\%}}$ 1 735 3 418 18 0
✅ php-parser 226 7.6 $\color{gray}{\textsf{+1.2\%}}$ 17 113 21 177 0 0
✅ tempest 898 20.5 $\color{gray}{\textsf{+0.9\%}}$ 5 091 10 536 275 0
✅ amphp-amp 48 1.6 $\color{gray}{\textsf{-3.1\%}}$ 31 $\color{red}{\textsf{+2}}$ $\color{gray}{\textsf{-0}}$ 53 $\color{red}{\textsf{+4}}$ 0 0
✅ webmozarts-assert 274 4.8 $\color{gray}{\textsf{+0.8\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 1.8 $\color{gray}{\textsf{+2.3\%}}$ 155 433 2 0
✅ monolog 136 5.9 $\color{gray}{\textsf{-0.7\%}}$ 918 2 491 11 0
✅ drupal 4 847 204.9 $\color{gray}{\textsf{-0.2\%}}$ 97 872 169 393 575 0
✅ wordpress 2 061 194.2 $\color{gray}{\textsf{-3.5\%}}$ 143 627 678 490 505 1
Total 15 382 783.6 $\color{gray}{\textsf{-0.7\%}}$ 349 481 $\color{red}{\textsf{+2}}$ $\color{green}{\textsf{-2}}$ 1 054 016 $\color{green}{\textsf{-1}}$ 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.
✅ symfony: issues +0 −2

Memory 3 619 → 3 619 MB (<0.1 %) · Wall 186.7 → 187.9 s (+0.7 %)

Weighted 100 455 → 100 450 (−5, <0.01 %): L2 −1 · L3 −1

Issue type added removed
PossiblyNullPropertyAssignmentValue 0 1
PropertyNotSetInConstructor 0 1

Removed (2)

  • src/Symfony/Component/Validator/Constraints/CssColor.php:62 PropertyNotSetInConstructor (error): Property CssColor::$formats is not defined in constructor of CssColor or in any methods called in the constructor
  • src/Symfony/Component/Validator/Constraints/CssColor.php:90 PossiblyNullPropertyAssignmentValue (error): $this->formats with non-nullable declared type 'array<array-key, mixed>|string' cannot be assigned nullable type 'array<array-key, mixed|string>|null|string'
✅ amphp-amp: issues +2 −0

Memory 48 → 48 MB (+0.0 %) · Wall 1.6 → 1.6 s (−3.1 %)

Weighted 49 → 53 (+4, +8.16 %): L2 +2

Issue type added removed
RedundantConditionGivenDocblockType 2 0

Added (2)

  • src/SignalCancellation.php:41 RedundantConditionGivenDocblockType (error): Operand of type non-empty-list<array{args?: list<mixed>, class?: class-string, file?: string, function: string, line?: int, object?: object, type?: string}> is always truthy
  • src/TimeoutCancellation.php:30 RedundantConditionGivenDocblockType (error): Operand of type non-empty-list<array{args?: list<mixed>, class?: class-string, file?: string, function: string, line?: int, object?: object, type?: string}> is always truthy

head ecdaa53 · base eda66f2 (merge-base) · workflow run

…null-narrowing

# Conflicts:
#	tests/TypeReconciliation/IssetTest.php
@orklah

orklah commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks! This is ready to merge, but it now conflicts with master in tests/TypeReconciliation/IssetTest.php after today's merges. Could you merge master (keeping both sides)? I'll merge it once CI is green.

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 24, 2026
@orklah

orklah commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit a465ec4 into vimeo:master Nov 24, 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.

($a['k'] ?? null) !== null does not narrow a repeat of the same ?? expression

2 participants