← All results

Post-call narrowing via self-out annotations

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

References: - Psalm @psalm-this-out - PHPStan @phpstan-self-out - Mago removed @this-out support as unsound

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_this_out_self_out.php:33: Template type U not used in return value of function/method replace() [PhanTemplateTypeNotUsedInFunctionReturn]
assertions_this_out_self_out.php:53: Argument 1 ($box) is $box of type \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox|\Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<1> but \Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox() takes \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> (real type \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/assertions_this_out_self_out.php:44 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
assertions_this_out_self_out.php:57: Argument 1 ($box) is $box of type \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox|\Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<1> but \Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox() takes \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> (real type \Conformance\Tests\AssertionsThisOutSelfOut\MutableBox) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/assertions_this_out_self_out.php:44 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.5+
assertions_this_out_self_out.php:53: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int> given. [identifier=argument.type] [reported-from-level=5]
assertions_this_out_self_out.php:58: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesIntBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
assertions_this_out_self_out.php:53: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int> given. [identifier=argument.type]
assertions_this_out_self_out.php:58: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesIntBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> given. [identifier=argument.type]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
assertions_this_out_self_out.php:53: Argument 1 of Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>, but Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int> provided [InvalidArgument]
assertions_this_out_self_out.php:58: Argument 1 of Conformance\Tests\AssertionsThisOutSelfOut\takesIntBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>, but Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> provided [InvalidArgument]
mago 1.46.0mago 1.46.0 Pass
assertions_this_out_self_out.php:53: Invalid argument type for argument #1 of `Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox`: expected `Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>`, but found `Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>`. [invalid-argument]
assertions_this_out_self_out.php:57: Invalid argument type for argument #1 of `Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox`: expected `Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>`, but found `Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>`. [invalid-argument]
mir 0.70.1mir 0.70.1 Pass
assertions_this_out_self_out.php:53: InvalidArgument: Argument $box of takesStringBox() expects 'Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>', got 'Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>' [MIR0201]
phpantom 0.9.0phpantom_lsp 0.9.0 Fail

No diagnostics reported.

Expectation diff
Line 53: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 53: Expected 1 error(s)
phpy 0.2.0phpy 0.2.0 Fail

No diagnostics reported.

Expectation diff
Line 53: Expected 1 error(s)
qodana 262.8665.325Qodana 262.8665.325 Fail

No diagnostics reported.

Expectation diff
Line 53: 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_this_out_self_out.php:23: param $value miss matched with phpdoc type <<T>> [funcParamTypeMissMatch]
assertions_this_out_self_out.php:29: Class or interface named \Conformance\Tests\AssertionsThisOutSelfOut\U does not exist [undefinedClass]
assertions_this_out_self_out.php:33: param $value miss matched with phpdoc type <<U>> [funcParamTypeMissMatch]
assertions_this_out_self_out.php:39: param $box miss matched with phpdoc type <<MutableBox<int>>> [funcParamTypeMissMatch]
assertions_this_out_self_out.php:44: param $box miss matched with phpdoc type <<MutableBox<string>>> [funcParamTypeMissMatch]
Expectation diff
Line 53: Expected 1 error(s)
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
assertions_this_out_self_out.php:53: argument new MutableBox() to takesStringBox() violates declared @param MutableBox<string> $box — declared contract violation [phpdoc.param-mismatch]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
assertions_this_out_self_out.php:53: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int> given. [identifier=argument.type]
assertions_this_out_self_out.php:58: Parameter #1 $box of function Conformance\Tests\AssertionsThisOutSelfOut\takesIntBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>, Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> given. [identifier=argument.type]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Pass
assertions_this_out_self_out.php:53: Argument 1 of Conformance\Tests\AssertionsThisOutSelfOut\takesStringBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string>, but Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int> provided [InvalidArgument]
assertions_this_out_self_out.php:58: Argument 1 of Conformance\Tests\AssertionsThisOutSelfOut\takesIntBox expects Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<int>, but Conformance\Tests\AssertionsThisOutSelfOut\MutableBox<string> provided [InvalidArgument]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsThisOutSelfOut;

/**
 * Post-call narrowing via self-out annotations.
 *
 * References:
 * - Psalm `@psalm-this-out`
 * - PHPStan `@phpstan-self-out`
 * - Mago removed `@this-out` support as unsound
 */

/**
 * @template T
 */
final class MutableBox
{
    /** @param T $value */
    public function __construct(
        public mixed $value, // E<noverify>: NoVerify does not reconcile the promoted property with the template-bound constructor param
    ) {
    }

    /**
     * @template U
     * @param U $value // E<noverify>: NoVerify does not understand method-level template parameters here
     * @psalm-this-out self<U>
     * @phpstan-self-out self<U>
     */
    public function replace(mixed $value): void // E<phan>: Phan requires method template parameters to appear in the return type // E<noverify>: NoVerify treats the method template param as an unresolved class name
    {
    }
}

/** @param MutableBox<int> $box */
function takesIntBox(MutableBox $box): void // E<noverify>: NoVerify does not accept this generic parameter annotation on the function parameter
{
}

/** @param MutableBox<string> $box */
function takesStringBox(MutableBox $box): void // E<noverify>: NoVerify does not accept this generic parameter annotation on the function parameter
{
}

function inspectReplacement(string $next): void
{
    $box = new MutableBox(1);

    takesIntBox($box);
    takesStringBox($box); // E: int box should not satisfy string box before replacement

    $box->replace($next);

    takesStringBox($box); // E<mago>: Mago intentionally does not honor self-out / this-out // E<phan>: Phan does not support self-out / this-out template updates
    takesIntBox($box); // E<phpstan>: replacement should update MutableBox<int> to MutableBox<string> // E<phpstan-strict>: replacement should update MutableBox<int> to MutableBox<string> // E<psalm>: replacement should update MutableBox<int> to MutableBox<string>
}