← 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.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Pass
assertions_null_guard.php:38: Call to method name on non-class type null [PhanNonClassMethodCall]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.2+
assertions_null_guard.php:38: Cannot call method name() on null. [identifier=method.nonObject] [reported-from-level=2]
With strict-rules
assertions_null_guard.php:38: Cannot call method name() on null. [identifier=method.nonObject]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
assertions_null_guard.php:38: Cannot call method name on null value [NullReference]
mago 1.46.0mago 1.46.0 Pass
assertions_null_guard.php:38: Attempting to call a method on `null`. [method-access-on-null]
mir 0.70.1mir 0.70.1 Pass
assertions_null_guard.php:38: NullMethodCall: Cannot call method name() on null [MIR0102]
phpantom 0.9.0phpantom_lsp 0.9.0 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]
phpy 0.2.0phpy 0.2.0 Pass
assertions_null_guard.php:38: Call to a member function name() on a non-object of type null
qodana 262.8665.325Qodana 262.8665.325 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.5steins 0.1.5 (2026-08-12 revision 39b4cc1) 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.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
assertions_null_guard.php:38: Cannot call method name() on null. [identifier=method.nonObject]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 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
}