← All results

Null-check narrowing

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

References: - common analyzer narrowing on null checks - python-typing narrowing groups

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
assertions_null_guard.php:38: Call to method name on non-class type null [PhanNonClassMethodCall]
phpstan 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass reported Lv.2+
assertions_null_guard.php:38: Cannot call method name() on null. [reported-from-level=2]
With strict-rules
assertions_null_guard.php:38: Cannot call method name() on null.
psalm 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08
next: 7.0.4
Pass
assertions_null_guard.php:38: Cannot call method name on null value [NullReference]
mago 1.59.0mago 1.59.0 Pass
assertions_null_guard.php:38: Attempting to call a method on `null`. [method-access-on-null]
mir 0.91.0mir 0.91.0 Pass
assertions_null_guard.php:38: NullMethodCall: Cannot call method name() on null [MIR0102]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
assertions_null_guard.php:38: Cannot access method 'name' on type 'null' [scalar_member_access]
intelephense 1.18.5intelephense 1.18.5 Pass
assertions_null_guard.php:38: Expected type 'object'. Found 'null'. [P1006]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 38: Expected 1 error(s)
phpy 1.0.19490phpy 1.0.19490 Pass
assertions_null_guard.php:38: Call to a member function name() on a non-object of type null
qodana 262.11879.104PhpStorm 262.11879.104 Pass
assertions_null_guard.php:38: '$user' is always 'null' [PhpExpressionAlwaysNullInspection]
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
assertions_null_guard.php:29: potential null dereference in user when accessing method [notNullSafetyVariable]
assertions_null_guard.php:38: potential null dereference in user when accessing method [notNullSafetyVariable]
Expectation diff
Line 29: Unexpected errors ["potential null dereference in user when accessing method [notNullSafetyVariable]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Pass
assertions_null_guard.php:38: method call $user->name() — $user is proven null on this path — proven Error (Call to a member function on null) [call.on-null]
phpstan-strict 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass
assertions_null_guard.php:38: Cannot call method name() on null.
psalm-next 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f Pass
assertions_null_guard.php:38: Cannot call method name on null value [NullReference]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsNullGuard;

/**
 * Null-check narrowing.
 *
 * References:
 * - common analyzer narrowing on null checks
 * - python-typing narrowing groups
 */

final class User
{
    public function name(): string
    {
        return 'user';
    }
}

function takesMaybeUser(?User $user): void
{
    if ($user === null) {
        return;
    }

    echo $user->name();
}

function usesNullBranch(?User $user): void
{
    if ($user !== null) {
        return;
    }

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