← 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.10Phan 6.0.10 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.1PHPStan - PHP Static Analysis Tool 2.3.1 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.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass
assertions_null_guard.php:38: Cannot call method name on null value [NullReference]
mago 1.57.0mago 1.57.0 Pass
assertions_null_guard.php:38: Attempting to call a method on `null`. [method-access-on-null]
mir 0.87.0mir 0.87.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.19409phpy 1.0.19409 Pass
assertions_null_guard.php:38: Call to a member function name() on a non-object of type null
qodana 262.10968.76PhpStorm 262.10968.76 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.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
assertions_null_guard.php:38: Cannot call method name() on null.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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
}