Skip to content

fix(assertion): instanceof parent narrows to the parent class - #14318

Merged
orklah merged 6 commits into
vimeo:masterfrom
jirkace:fix/14246-instanceof-parent
Nov 23, 2026
Merged

orklah merged 6 commits into
vimeo:masterfrom
jirkace:fix/14246-instanceof-parent

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

$x instanceof parent does not narrow $x, although instanceof self and instanceof Box do.

Before

<?php
interface Shape {}

class Box implements Shape {
    public function getSize(): int {
        return 1;
    }
}

final class TaggedBox extends Box {
    public function narrowed(Shape $shape): int {
        if ($shape instanceof parent) {
            return $shape->getSize(); // UndefinedInterfaceMethod: Method Shape::getSize does not exist
        }

        return 0;
    }
}

After

            return $shape->getSize(); // no issue ($shape is Box)

Fixes #14246

…4246

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: decreased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 768 72.5 25 691 52 994 682 0
✅ symfony 3 596 270.7 $\color{gray}{\textsf{-2.6\%}}$ 50 600 $\color{red}{\textsf{+4}}$ $\color{green}{\textsf{-8}}$ 98 370 $\color{green}{\textsf{-16}}$ 1 528 0
✅ doctrine-orm 380 20.0 $\color{gray}{\textsf{-0.8\%}}$ 3 476 6 143 0 0
✅ psl 617 25.7 1 375 5 612 1 0
✅ phpunit 455 20.1 $\color{gray}{\textsf{+1.4\%}}$ 1 731 3 410 18 0
✅ php-parser 226 13.8 $\color{gray}{\textsf{+1.3\%}}$ 17 118 21 193 0 0
✅ tempest 895 37.5 $\color{gray}{\textsf{-0.1\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.4 $\color{gray}{\textsf{-1.4\%}}$ 29 49 0 0
✅ webmozarts-assert 272 4.4 $\color{gray}{\textsf{-0.7\%}}$ 575 2 714 0 0
✅ guzzle-promises 44 1.5 155 434 2 0
✅ monolog 135 5.7 $\color{gray}{\textsf{-0.5\%}}$ 913 2 479 10 0
✅ drupal 4 839 152.8 $\color{gray}{\textsf{+1.9\%}}$ 97 575 169 202 583 0
✅ wordpress 2 038 186.3 $\color{gray}{\textsf{+0.1\%}}$ 142 681 676 628 461 1
Total 15 312 812.4 $\color{gray}{\textsf{-0.5\%}}$ 346 966 $\color{red}{\textsf{+4}}$ $\color{green}{\textsf{-8}}$ 1 049 756 $\color{green}{\textsf{-16}}$ 3 537 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 +4 −8

Memory 3 596 → 3 596 MB (<0.1 %) · Wall 277.9 → 270.7 s (−2.6 %)

Weighted 98 386 → 98 370 (−16, −0.02 %): L1 −2 · L2 +2 · L3 −2 · L6 −2

Issue type added removed
DocblockTypeContradiction 2 0
InvalidPropertyFetch 0 2
MixedArgument 2 2
MixedAssignment 0 2
PossiblyUndefinedMethod 0 2

Changed in place (2)

@​@​ 2 places MixedArgument @​@​
  src/Symfony/Component/String/AbstractUnicodeString.php:499
  src/Symfony/Component/String/AbstractUnicodeString.php:538
- Argument 1 of preg_quote cannot be mixed, expecting string
+ Argument 1 of preg_quote cannot be mixed|string, expecting string

Added (2)

  • src/Symfony/Component/String/AbstractUnicodeString.php:495 DocblockTypeContradiction (error): Cannot resolve types for $prefix - docblock-defined type array<array-key, string>|string does not contain AbstractString
  • src/Symfony/Component/String/AbstractUnicodeString.php:534 DocblockTypeContradiction (error): Cannot resolve types for $suffix - docblock-defined type array<array-key, string>|string does not contain AbstractString

Removed (6)

  • src/Symfony/Component/Console/Command/LazyCommand.php:55 PossiblyUndefinedMethod (error): Method Closure::setApplication does not exist
  • src/Symfony/Component/Console/Command/LazyCommand.php:64 PossiblyUndefinedMethod (error): Method Closure::setHelperSet does not exist
  • src/Symfony/Component/String/AbstractUnicodeString.php:496 MixedAssignment (error): Unable to determine the type that $prefix is being assigned to
  • src/Symfony/Component/String/AbstractUnicodeString.php:496 InvalidPropertyFetch (error): Cannot fetch property on non-object $prefix of type array<array-key, string>
  • src/Symfony/Component/String/AbstractUnicodeString.php:535 MixedAssignment (error): Unable to determine the type that $suffix is being assigned to
  • src/Symfony/Component/String/AbstractUnicodeString.php:535 InvalidPropertyFetch (error): Cannot fetch property on non-object $suffix of type array<array-key, string>

head 6d0cf66 · base 28e192b (merge-base) · workflow run

@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • $x instanceof parent narrows $x to the parent class (and its negation excludes it), as instanceof self does. It was ignored before. Outside a class with a parent nothing changes.
  • Not measured: benchmark projects; not run: the full suite (CI).

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

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the fix is in the right place. Two things before merging:

  1. getParentFQCLN() returns null inside a trait method, so instanceof parent still doesn't narrow there, while self and parent::foo() are resolved against the using class:
trait T {
    public function viaTrait(Shape $s): int {
        if ($s instanceof parent) {
            return $s->getSize(); // still UndefinedInterfaceMethod
        }
        return 0;
    }
}
final class UsesTrait extends Box { use T; }

Resolving the parent from the storage of $this_class_name (as StaticCallAnalyzer does for parent:: through $context->self) fixes it and keeps every other case the same:

&& $this_class_name !== null
&& $source instanceof StatementsAnalyzer
&& ($parent_class_name = $source->getCodebase()->classlike_storage_provider->get($this_class_name)->parent_class) !== null
  1. The same gap exists next door: is_a($s, parent::class) (the // do nothing branch in the is_a handling) and get_class($s) === parent::class / $s::class === parent::class (getGetclassComparisonAssertions() sets parent to null). All three still report UndefinedInterfaceMethod on $s->getSize(). Could you handle them with the same resolution, with a test case each?

Minor: the test doesn't need php_version => '8.3' (and then MissingOverrideAttribute doesn't need ignoring), and a /** @psalm-check-type-exact $shape = Box */ in the narrowed branch would pin the type.

jirkace and others added 2 commits November 21, 2026 16:42
… for is_a() and class comparisons

instanceof parent, is_a($x, parent::class), get_class($x) === parent::class and
$x::class === parent::class resolve parent from the storage of the class the code runs
for, as self does.

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

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 5122297.

  1. parent is resolved from the storage of $this_class_name (AssertionFinder::getParentClassName()), so instanceof parent narrows in a trait method of a class that has a parent (parentOfTheClassUsingATraitNarrowsInTheTraitCode, fails on the previous head).
  2. is_a($s, parent::class), get_class($s) === parent::class and $s::class === parent::class use the same resolution, one case each (isAParentClass..., getClassIdenticalToParentClass..., classConstantIdenticalToParentClass...), all failing before with UndefinedInterfaceMethod.
  3. The test needs no php_version or ignored issue anymore, and the narrowed branch has @psalm-check-type-exact $shape = Box.

Not touched: parent::class written inside a trait still reports ParentNotFound on master (ClassConstAnalyzer), so those three are tested in a class. @orklah

…4246

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

orklah commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the three points are addressed and this is ready to merge, but it now conflicts with master after today's merges. Could you merge master (keeping both sides)? I'll merge it once CI is green.

# Conflicts:
#	tests/TypeReconciliation/ConditionalTest.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 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @jirkace!

@orklah
orklah merged commit 4066c2a into vimeo:master Nov 23, 2026
116 of 118 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.

instanceof parent does not narrow

2 participants