← All results

Unknown method on an instanceof intersection

Group: Intersection types · Category: language · File: intersections_instanceof_unknown_method.php

$base instanceof SomeInterface on a non-final class narrows to the intersection Base&SomeInterface. Members of both sides are available; a member of neither is still an error. A tool that gives up on the intersection and goes silent passes the controls but misses the probe. NamedAccount makes the intersection inhabited, so a closed-world tool that enumerates subclasses still sees a reachable branch. References: - intersections_interface_merge (declared intersections)

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
intersections_instanceof_unknown_method.php:55: Call to undeclared method \Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account::missing [PhanUndeclaredMethod]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.2+
intersections_instanceof_unknown_method.php:55: Call to an undefined method Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named::missing(). [reported-from-level=2]
intersections_instanceof_unknown_method.php:55: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
intersections_instanceof_unknown_method.php:55: Call to an undefined method Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named::missing().
intersections_instanceof_unknown_method.php:55: Parameter #1 (mixed) of echo cannot be converted to string.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass
intersections_instanceof_unknown_method.php:55: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
intersections_instanceof_unknown_method.php:55: Method (Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named)::missing does not exist [UndefinedMethod]
mago 1.57.0mago 1.57.0 Pass
intersections_instanceof_unknown_method.php:55: Method `missing` does not exist on type `Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account`. [non-existent-method]
intersections_instanceof_unknown_method.php:55: The first value for `echo` is too general. [mixed-argument]
mir 0.87.0mir 0.87.0 Pass
intersections_instanceof_unknown_method.php:55: UndefinedMethod: Method Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named::missing() does not exist [MIR0004]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
intersections_instanceof_unknown_method.php:55: Method 'missing' not found on any of the 2 possible types (Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account, Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named) [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Pass
intersections_instanceof_unknown_method.php:55: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
intersections_instanceof_unknown_method.php:55: Undefined method 'missing'. [P1013]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 55: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Pass
intersections_instanceof_unknown_method.php:55: Call to unknown method: Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account|Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named::missing()
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 55: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
intersections_instanceof_unknown_method.php:48: Call to undefined method {\Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named}->id() [undefinedMethod]
intersections_instanceof_unknown_method.php:55: Call to undefined method {\Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named}->missing() [undefinedMethod]
Expectation diff
Line 48: Expected valid value to be accepted, got ["Call to undefined method {\\Conformance\\Tests\\IntersectionsInstanceofUnknownMethod\\Named}->id() [undefinedMethod]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Pass
intersections_instanceof_unknown_method.php:55: call to undefined method Account::missing() — declared receiver $account narrowed to {Account}, hierarchy and descendants fully enumerated, no __call, no @method/@property/@mixin [call.undefined-method]
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
intersections_instanceof_unknown_method.php:55: Call to an undefined method Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named::missing().
intersections_instanceof_unknown_method.php:55: Parameter #1 (mixed) of echo cannot be converted to string.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
intersections_instanceof_unknown_method.php:55: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
intersections_instanceof_unknown_method.php:55: Method (Conformance\Tests\IntersectionsInstanceofUnknownMethod\Account&Conformance\Tests\IntersectionsInstanceofUnknownMethod\Named)::missing does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\IntersectionsInstanceofUnknownMethod;

/**
 * Unknown method on an `instanceof` intersection.
 *
 * `$base instanceof SomeInterface` on a non-final class narrows to the
 * intersection `Base&SomeInterface`. Members of both sides are available;
 * a member of neither is still an error. A tool that gives up on the
 * intersection and goes silent passes the controls but misses the probe.
 *
 * NamedAccount makes the intersection inhabited, so a closed-world tool that
 * enumerates subclasses still sees a reachable branch.
 *
 * References:
 * - intersections_interface_merge (declared intersections)
 */

interface Named
{
    public function name(): string;
}

class Account
{
    public function id(): int
    {
        return 1;
    }
}

final class NamedAccount extends Account implements Named
{
    #[\Override]
    public function name(): string
    {
        return 'named';
    }
}

function narrows(Account $account): void
{
    if ($account instanceof Named) {
        echo $account->name(); // V: from the interface
        echo $account->id(); // V: from the class
    }
}

function narrowsMissing(Account $account): void
{
    if ($account instanceof Named) {
        echo $account->missing(); // E: neither side declares missing()
    }
}