← All results

A successful nullsafe access narrows the receiver to non-null

Group: Object compatibility · Category: language · File: objects_nullsafe_narrowing.php

After if ($user?->name !== null), $user is User, so honour is silence on the guarded read. The unguarded $user->name is the reference probe: it shows the tool reports a nullable receiver at all, without which silence on the guarded read would prove nothing. $user = new User() would make both sides silent. Source: phan/phan#5569 (6.0.8). References: - phan#5569

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 Fail

No diagnostics reported.

Expectation diff
Line 43: Expected 1 error(s)
phpstan 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass reported Lv.8+
objects_nullsafe_narrowing.php:43: Cannot access property $name on Conformance\Tests\ObjectsNullsafeNarrowing\User|null. [reported-from-level=8]
With strict-rules
objects_nullsafe_narrowing.php:43: Cannot access property $name on Conformance\Tests\ObjectsNullsafeNarrowing\User|null.
psalm 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08
next: 7.0.4
Fail (pzoom≠)
objects_nullsafe_narrowing.php:37: Cannot get property on possibly null variable $user of type Conformance\Tests\ObjectsNullsafeNarrowing\User|null [PossiblyNullPropertyFetch]
objects_nullsafe_narrowing.php:37: Argument 1 of Conformance\Tests\ObjectsNullsafeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]
objects_nullsafe_narrowing.php:43: Cannot get property on possibly null variable $user of type Conformance\Tests\ObjectsNullsafeNarrowing\User|null [PossiblyNullPropertyFetch]
objects_nullsafe_narrowing.php:43: Argument 1 of Conformance\Tests\ObjectsNullsafeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]
pzoom (Psalm port)
objects_nullsafe_narrowing.php:43: Argument 1 of takesString expects string, but possibly different type null|string provided [PossiblyNullArgument]
objects_nullsafe_narrowing.php:43: Cannot access property $name on possibly null type [PossiblyNullPropertyFetch]
Expectation diff
Line 37: Expected silence (quiet probe), got ["Cannot get property on possibly null variable $user of type Conformance\\Tests\\ObjectsNullsafeNarrowing\\User|null [PossiblyNullPropertyFetch]","Argument 1 of Conformance\\Tests\\ObjectsNullsafeNarrowing\\takesString cannot be null, possibly null value provided [PossiblyNullArgument]"]
mago 1.59.0mago 1.59.0 Pass
objects_nullsafe_narrowing.php:43: Attempting to access a property on a possibly `null` value. [possibly-null-property-access]
objects_nullsafe_narrowing.php:43: Argument #1 of function `Conformance\Tests\ObjectsNullsafeNarrowing\takesString` is possibly `null`, but parameter type `string` does not accept it. [possibly-null-argument]
mir 0.91.0mir 0.91.0 Pass
objects_nullsafe_narrowing.php:43: PossiblyNullPropertyFetch: Cannot access property $name on possibly null value [MIR0106]
objects_nullsafe_narrowing.php:43: PossiblyNullArgument: Argument $value of takesString() might be null [MIR0104]
phpantom 0.11.1phpantom_lsp 0.11.1 Fail

No diagnostics reported.

Expectation diff
Line 43: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Pass
objects_nullsafe_narrowing.php:43: Expected type 'object'. Found 'null|Conformance\Tests\ObjectsNullsafeNarrowing\User'. [P1006]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 43: Expected 1 error(s)
phpy 1.0.19490phpy 1.0.19490 Fail

No diagnostics reported.

Expectation diff
Line 43: Expected 1 error(s)
qodana 262.11879.104PhpStorm 262.11879.104 Fail

No diagnostics reported.

Expectation diff
Line 43: 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
objects_nullsafe_narrowing.php:37: potential null dereference when accessing variable 'user' [notSafeCall]
objects_nullsafe_narrowing.php:37: potential attempt to access property through null [notNullSafetyPropertyFetch]
objects_nullsafe_narrowing.php:43: potential null dereference when accessing variable 'user' [notSafeCall]
objects_nullsafe_narrowing.php:43: potential attempt to access property through null [notNullSafetyPropertyFetch]
Expectation diff
Line 37: Expected silence (quiet probe), got ["potential null dereference when accessing variable 'user' [notSafeCall]","potential attempt to access property through null [notNullSafetyPropertyFetch]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 43: Expected 1 error(s)
phpstan-strict 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass
objects_nullsafe_narrowing.php:43: Cannot access property $name on Conformance\Tests\ObjectsNullsafeNarrowing\User|null.
psalm-next 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f Fail
objects_nullsafe_narrowing.php:37: Cannot get property on possibly null variable $user of type Conformance\Tests\ObjectsNullsafeNarrowing\User|null [PossiblyNullPropertyFetch]
objects_nullsafe_narrowing.php:37: Argument 1 of Conformance\Tests\ObjectsNullsafeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]
objects_nullsafe_narrowing.php:43: Cannot get property on possibly null variable $user of type Conformance\Tests\ObjectsNullsafeNarrowing\User|null [PossiblyNullPropertyFetch]
objects_nullsafe_narrowing.php:43: Argument 1 of Conformance\Tests\ObjectsNullsafeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]
Expectation diff
Line 37: Expected silence (quiet probe), got ["Cannot get property on possibly null variable $user of type Conformance\\Tests\\ObjectsNullsafeNarrowing\\User|null [PossiblyNullPropertyFetch]","Argument 1 of Conformance\\Tests\\ObjectsNullsafeNarrowing\\takesString cannot be null, possibly null value provided [PossiblyNullArgument]"]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ObjectsNullsafeNarrowing;

/**
 * A successful nullsafe access narrows the receiver to non-null.
 *
 * After `if ($user?->name !== null)`, `$user` is `User`, so honour is
 * silence on the guarded read. The unguarded `$user->name` is the reference
 * probe: it shows the tool reports a nullable receiver at all, without which
 * silence on the guarded read would prove nothing. `$user = new User()`
 * would make both sides silent.
 *
 * Source: phan/phan#5569 (6.0.8).
 *
 * References:
 * - https://github.com/phan/phan/pull/5569
 */

final class User
{
    public function __construct(
        public string $name,
    ) {
    }
}

function takesString(string $value): void
{
}

function afterNullsafe(?User $user): void
{
    if ($user?->name !== null) {
        takesString($user->name); // Q: the receiver is non-null
    }
}

function unguarded(?User $user): void
{
    takesString($user->name); // E: ?User is not User without a guard
}