Skip to content

A possibly undefined variable reads and reconciles as null when undefined - #13762

Merged
orklah merged 4 commits into
vimeo:masterfrom
orklah:possibly-undefined-reads-as-null
Nov 14, 2026
Merged

orklah merged 4 commits into
vimeo:masterfrom
orklah:possibly-undefined-reads-as-null

Conversation

@orklah

@orklah orklah commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

A variable a previous loop iteration defines is treated as never null when it may be undefined, although reading an undefined variable gives null.

Before

<?php
function notNull(array $xs): void {
    foreach ($xs as $_) {
        if ($item !== null) { // PossiblyUndefinedVariable, RedundantCondition: Item can never contain null
            echo $item->name(); // PossiblyUndefinedVariable
        }
        $item = new Item();
    }
}

function falsy(array $xs): void {
    foreach ($xs as $_) {
        if (!$a) { // PossiblyUndefinedVariable, TypeDoesNotContainType: Operand of type false is always falsy
            /** @psalm-check-type-exact $a? = null */; // CheckType
        }
        $a = "s";
    }
}

function notIsset(array $xs): void {
    foreach ($xs as $_) {
        if (!isset($a)) {
            /** @psalm-check-type-exact $a? = null */; // CheckType: $a = null
            echo (string) $a;
        }
        $a = "s";
    }
}

After

        if ($item !== null) { // PossiblyUndefinedVariable
            echo $item->name(); // no issue
        if (!$a) { // PossiblyUndefinedVariable
            /** @psalm-check-type-exact $a? = null */; // no issue
            /** @psalm-check-type-exact $a? = null */; // no issue
            echo (string) $a; // PossiblyUndefinedVariable

…ined

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 14, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T20:09:12.836962Z 452359a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 452359a. Configure here.

Comment thread src/Psalm/Type/Reconciler.php Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 452359adb6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Psalm/Type/Reconciler.php Resolved
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Issues: increased (+25 −7)

Project Memory (MB) Wall (s) Issues Weighted
✅ laravel 1 691 53.0 $\color{gray}{\textsf{-2.6\%}}$ 26 056 50 823
✅ symfony 3 593 197.9 $\color{gray}{\textsf{+5.5\%}}$ 50 944 $\color{red}{\textsf{+16}}$ $\color{green}{\textsf{-5}}$ 100 068 $\color{red}{\textsf{+29}}$
✅ doctrine-orm 391 14.2 $\color{gray}{\textsf{-0.2\%}}$ 3 537 5 608
✅ psl 628 18.0 $\color{gray}{\textsf{+0.2\%}}$ 1 337 5 262
✅ phpunit 470 12.3 $\color{gray}{\textsf{-2.1\%}}$ 1 611 1 216
✅ php-parser 231 8.1 $\color{gray}{\textsf{+0.1\%}}$ 17 148 $\color{red}{\textsf{+7}}$ $\color{green}{\textsf{-2}}$ 20 553 $\color{red}{\textsf{+9}}$
✅ tempest 886 22.1 $\color{gray}{\textsf{-1.2\%}}$ 4 909 9 152
✅ amphp-amp 61 2.2 $\color{gray}{\textsf{-1.8\%}}$ 32 79
✅ webmozarts-assert 279 5.1 $\color{gray}{\textsf{-0.2\%}}$ 577 2 714
✅ guzzle-promises 55 2.2 $\color{gray}{\textsf{+0.5\%}}$ 161 402
✅ monolog 153 6.0 914 2 185
✅ drupal 4 740 157.7 $\color{gray}{\textsf{-0.7\%}}$ 98 378 $\color{red}{\textsf{+2}}$ $\color{gray}{\textsf{-0}}$ 160 578 $\color{red}{\textsf{+6}}$
Total 13 179 498.9 $\color{gray}{\textsf{+1.5\%}}$ 205 604 $\color{red}{\textsf{+25}}$ $\color{green}{\textsf{-7}}$ 358 640 $\color{red}{\textsf{+44}}$
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, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted: issues weighted by error level (levels 1–8 as is, always-errors 10, feature-specific and purity suggestions 0). 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.
  • 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{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 ⚠️).
  • Issues (heading): the total weighted change, increased · decreased · unchanged, followed by the issues added and removed across the projects, e.g. increased (+175 −128). If the weighted total is zero but issues were added or removed (those weighted 0 changed, or changes offset), it reads changed (+175 −128, weighted unchanged). Informational, never fails the check.
✅ symfony: issues +16 −5

Memory 3 593 → 3 593 MB (<0.1 %) · Wall 187.5 → 197.9 s (+5.5 %)

Weighted 100 039 → 100 068 (+29, +0.03 %): L1 +1 · L2 +2 · L3 +8

Issue type added removed
ArgumentTypeCoercion 3 3
MixedArgument 1 1
MixedArgumentTypeCoercion 1 1
PossiblyNullArgument 8 0
PossiblyNullOperand 1 0
RiskyTruthyFalsyComparison 2 0

Added (16)

  • src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php:399 PossiblyNullArgument (error): Argument 3 of proc_open cannot be null, possibly null value provided
  • src/Symfony/Component/Console/ArgumentResolver/ArgumentResolver.php:126 RiskyTruthyFalsyComparison (error): Operand of type int<0, max>|null contains type int<0, max>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • src/Symfony/Component/DependencyInjection/Compiler/ResolveDecoratorStackPass.php:123 MixedArgumentTypeCoercion (error): Argument 2 of Symfony\Component\DependencyInjection\ContainerBuilder::setAlias expects Symfony\Component\DependencyInjection\Alias|string, but parent type Symfony\Component\Depend…
  • src/Symfony/Component/DependencyInjection/Compiler/ResolveDecoratorStackPass.php:123 PossiblyNullArgument (error): Argument 2 of Symfony\Component\DependencyInjection\ContainerBuilder::setAlias cannot be null, possibly null value provided
  • src/Symfony/Component/DomCrawler/Crawler.php:510 MixedArgument (error): Argument 2 of Symfony\Component\DomCrawler\Crawler::matchingNodes cannot be DOMNode|mixed|null, expecting DOMNode
  • src/Symfony/Component/DomCrawler/Crawler.php:510 PossiblyNullArgument (error): Argument 2 of Symfony\Component\DomCrawler\Crawler::matchingNodes cannot be null, possibly null value provided
  • src/Symfony/Component/Emoji/EmojiTransliterator.php:202 PossiblyNullOperand (error): Right operand cannot be nullable, got int|null
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\AmpResponse::select expects Symfony\Component\HttpClient\Internal\AmpClientState, but parent type Symfony\Component\HttpClient\Internal\ClientSt…
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\CurlResponse::select expects Symfony\Component\HttpClient\Internal\CurlClientState, but parent type Symfony\Component\HttpClient\Internal\Client…
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\NativeResponse::select expects Symfony\Component\HttpClient\Internal\NativeClientState, but parent type Symfony\Component\HttpClient\Internal\Cl…
  • … and 6 more

Removed (5)

  • src/Symfony/Component/DependencyInjection/Compiler/ResolveDecoratorStackPass.php:123 MixedArgumentTypeCoercion (error): Argument 2 of Symfony\Component\DependencyInjection\ContainerBuilder::setAlias expects Symfony\Component\DependencyInjection\Alias|string, but parent type Symfony\Component\Depend…
  • src/Symfony/Component/DomCrawler/Crawler.php:510 MixedArgument (error): Argument 2 of Symfony\Component\DomCrawler\Crawler::matchingNodes cannot be DOMNode|mixed, expecting DOMNode
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\AmpResponse::select expects Symfony\Component\HttpClient\Internal\AmpClientState, but parent type Symfony\Component\HttpClient\Internal\ClientSt…
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\CurlResponse::select expects Symfony\Component\HttpClient\Internal\CurlClientState, but parent type Symfony\Component\HttpClient\Internal\Client…
  • src/Symfony/Component/HttpClient/Response/TransportResponseTrait.php:340 ArgumentTypeCoercion (error): Argument 1 of Symfony\Component\HttpClient\Response\NativeResponse::select expects Symfony\Component\HttpClient\Internal\NativeClientState, but parent type Symfony\Component\HttpClient\Internal\Cl…
✅ php-parser: issues +7 −2

Memory 231 → 231 MB (<0.1 %) · Wall 8.1 → 8.1 s (+0.1 %)

Weighted 20 544 → 20 553 (+9, +0.04 %): L1 +3 · L3 +2

Issue type added removed
MixedArgument 1 1
MixedArrayTypeCoercion 1 1
PossiblyNullArgument 1 0
PossiblyNullArrayOffset 1 0
PossiblyNullOperand 3 0

Added (7)

  • lib/PhpParser/ParserAbstract.php:350 PossiblyNullOperand (error): Right operand cannot be nullable, got int|mixed|null
  • lib/PhpParser/ParserAbstract.php:369 MixedArgument (error): Argument 2 of PhpParser\ParserAbstract::getErrorMessage cannot be int|mixed|null, expecting int
  • lib/PhpParser/ParserAbstract.php:369 PossiblyNullArgument (error): Argument 2 of PhpParser\ParserAbstract::getErrorMessage cannot be null, possibly null value provided
  • lib/PhpParser/ParserAbstract.php:379 MixedArrayTypeCoercion (error): Coercion from array offset type 'int|mixed|null' to the expected type 'array-key'
  • lib/PhpParser/ParserAbstract.php:379 PossiblyNullArrayOffset (error): Cannot access value on variable $this->actionBase using possibly null offset int|mixed|null
  • lib/PhpParser/ParserAbstract.php:382 PossiblyNullOperand (error): Left operand cannot be nullable, got int|mixed|null
  • lib/PhpParser/ParserAbstract.php:420 PossiblyNullOperand (error): Left operand cannot be nullable, got int|mixed|null

Removed (2)

  • lib/PhpParser/ParserAbstract.php:369 MixedArgument (error): Argument 2 of PhpParser\ParserAbstract::getErrorMessage cannot be int|mixed, expecting int
  • lib/PhpParser/ParserAbstract.php:379 MixedArrayTypeCoercion (error): Coercion from array offset type 'int|mixed' to the expected type 'array-key'
✅ drupal: issues +2 −0

Memory 4 740 → 4 740 MB (<0.1 %) · Wall 158.9 → 157.7 s (−0.7 %)

Weighted 160 572 → 160 578 (+6, <0.01 %): L3 +2

Issue type added removed
PossiblyUndefinedVariable 2 0

Added (2)

  • core/lib/Drupal/Core/DrupalKernel.php:1114 PossiblyUndefinedVariable (error): Possibly undefined variable $container_definition, first seen on line 1034
  • core/modules/file/file.module:493 PossiblyUndefinedVariable (error): Possibly undefined variable $file_fields, first seen on line 479

head 3d511f3 · base ce701c5 (merge-base) · workflow run

orklah and others added 3 commits November 14, 2026 21:09
…ation

The variable fetch already leaves it out; reconciling it as null dropped its
possibly_undefined_from_try flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s the condition

A mixed does not show whether reconciliation kept its null, so check whether
null itself passes the assertions.

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

Assigning an item to an undefined variable creates the array: the variable is
not read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant