← All results

Type checks narrow object properties the way they narrow locals

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

After is_string($this->name), $this->name is string for the rest of the branch. A tool that only narrows variables still sees string|null on the property and rejects takesString, so honour is silence on the guarded call. The unguarded call shows the tool reports string|null to string at all; without it, silence on the guarded call would prove nothing. Source: PHPantom-dev/phpantom_lsp#466 (0.11.1). References: - phpantom_lsp#466

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 42: Expected 1 error(s)
phpstan 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass reported Lv.8+
assertions_property_type_narrowing.php:42: Parameter #1 $value of function Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString expects string, string|null given. [reported-from-level=8]
With strict-rules
assertions_property_type_narrowing.php:42: Parameter #1 $value of function Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString expects string, string|null given.
psalm 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08
next: 7.0.4
Pass
assertions_property_type_narrowing.php:42: Argument 1 of Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]
mago 1.59.0mago 1.59.0 Pass
assertions_property_type_narrowing.php:42: Argument #1 of function `Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString` is possibly `null`, but parameter type `string` does not accept it. [possibly-null-argument]
mir 0.91.0mir 0.91.0 Pass
assertions_property_type_narrowing.php:42: PossiblyNullArgument: Argument $value of takesString() might be null [MIR0104]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
assertions_property_type_narrowing.php:42: Argument 1 ($value) expects string, got string|null (null does not satisfy string) [type_mismatch_argument]
intelephense 1.18.5intelephense 1.18.5 Pass
assertions_property_type_narrowing.php:42: Expected type 'string'. Found 'string|null'. [P1006]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

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

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 42: 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
assertions_property_type_narrowing.php:35: potential null dereference when accessing property 'name' [notNullSafetyFunctionArgumentPropertyFetch]
assertions_property_type_narrowing.php:42: potential null dereference when accessing property 'name' [notNullSafetyFunctionArgumentPropertyFetch]
Expectation diff
Line 35: Expected silence (quiet probe), got ["potential null dereference when accessing property 'name' [notNullSafetyFunctionArgumentPropertyFetch]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 42: Expected 1 error(s)
phpstan-strict 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass
assertions_property_type_narrowing.php:42: Parameter #1 $value of function Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString expects string, string|null given.
psalm-next 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f Pass
assertions_property_type_narrowing.php:42: Argument 1 of Conformance\Tests\AssertionsPropertyTypeNarrowing\takesString cannot be null, possibly null value provided [PossiblyNullArgument]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsPropertyTypeNarrowing;

/**
 * Type checks narrow object properties the way they narrow locals.
 *
 * After `is_string($this->name)`, `$this->name` is `string` for the rest of
 * the branch. A tool that only narrows variables still sees `string|null` on
 * the property and rejects `takesString`, so honour is silence on the guarded
 * call. The unguarded call shows the tool reports `string|null` to `string`
 * at all; without it, silence on the guarded call would prove nothing.
 *
 * Source: PHPantom-dev/phpantom_lsp#466 (0.11.1).
 *
 * References:
 * - https://github.com/PHPantom-dev/phpantom_lsp/pull/466
 */

final class Named
{
    public function __construct(
        private string|null $name,
    ) {
    }

    public function label(): string
    {
        if (!is_string($this->name)) {
            return '';
        }

        takesString($this->name); // Q: the property is string in this branch

        return $this->name;
    }

    public function unguarded(): void
    {
        takesString($this->name); // E: string|null is not string without the guard
    }
}

function takesString(string $value): void
{
}

function takesInt(int $value): void
{
}