← All results

Basic instanceof-based narrowing checks

Group: Assertions and narrowing · Category: ecosystem · File: assertions_instanceof_narrowing.php

References: - common analyzer narrowing on instanceof branches

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Pass
assertions_instanceof_narrowing.php:46: Call to undeclared method \Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name [PhanUndeclaredMethod]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.2+
assertions_instanceof_narrowing.php:46: Call to an undefined method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name(). [identifier=method.notFound] [reported-from-level=2]
With strict-rules
assertions_instanceof_narrowing.php:46: Call to an undefined method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name(). [identifier=method.notFound]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
assertions_instanceof_narrowing.php:46: Method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name does not exist [UndefinedMethod]
mago 1.46.0mago 1.46.0 Pass
assertions_instanceof_narrowing.php:46: Method `name` does not exist on type `Conformance\Tests\AssertionsInstanceofNarrowing\Guest`. [non-existent-method]
mir 0.70.1mir 0.70.1 Pass
assertions_instanceof_narrowing.php:46: UndefinedMethod: Method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name() does not exist [MIR0004]
phpantom 0.9.0phpantom_lsp 0.9.0 Pass
assertions_instanceof_narrowing.php:46: Method 'name' not found on class 'Conformance\Tests\AssertionsInstanceofNarrowing\Guest' [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Pass
assertions_instanceof_narrowing.php:46: Undefined method 'name'. [P1013]
phpy 0.2.0phpy 0.2.0 Pass
assertions_instanceof_narrowing.php:46: Call to unknown method: Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name()
qodana 262.8665.325Qodana 262.8665.325 Fail
assertions_instanceof_narrowing.php:37: Argument type does not match the declared [PhpDocSignatureInspection]
Expectation diff
Line 46: Expected 1 error(s)
Line 37: Unexpected errors ["Argument type does not match the declared [PhpDocSignatureInspection]"]
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
assertions_instanceof_narrowing.php:39: param $value miss matched with phpdoc type <<User|Guest>> [funcParamTypeMissMatch]
assertions_instanceof_narrowing.php:46: Call to undefined method {\Conformance\Tests\AssertionsInstanceofNarrowing\Guest|object}->name() [undefinedMethod]
Expectation diff
Line 39: Unexpected errors ["param $value miss matched with phpdoc type <<User|Guest>> [funcParamTypeMissMatch]"]
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
assertions_instanceof_narrowing.php:46: call to undefined method Guest::name() — declared receiver $value narrowed to {Guest}, hierarchy and descendants fully enumerated, no __call, no @method/@property/@mixin [phpdoc.undefined-method]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
assertions_instanceof_narrowing.php:46: Call to an undefined method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name(). [identifier=method.notFound]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Pass
assertions_instanceof_narrowing.php:46: Method Conformance\Tests\AssertionsInstanceofNarrowing\Guest::name does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsInstanceofNarrowing;

/**
 * Basic instanceof-based narrowing checks.
 *
 * References:
 * - common analyzer narrowing on instanceof branches
 */

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

final class User implements Named
{
    #[\Override]
    public function name(): string
    {
        return 'user';
    }
}

final class Guest
{
    public function guestId(): int
    {
        return 1;
    }
}

/**
 * @param User|Guest $value
 */
function takesUserOrGuest(object $value): void
{
    if ($value instanceof User) {
        echo $value->name();
        return;
    }

    $value->name(); // E: Guest branch should not expose User methods
}