← All results

Return type of an immediately invoked closure

Group: Callables · Category: language · File: callables_immediately_invoked_closure.php

An immediately invoked closure or arrow function, (fn (): T => …)(), evaluates to the closure's return type. The control assigns the closure first and calls the variable, which most tools already handle. References: - https://www.php.net/manual/en/functions.arrow.php

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
callables_immediately_invoked_closure.php:31: Call to undeclared method \Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing [PhanUndeclaredMethod]
callables_immediately_invoked_closure.php:39: Call to undeclared method \Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing [PhanUndeclaredMethod]
callables_immediately_invoked_closure.php:47: Call to undeclared method \Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing [PhanUndeclaredMethod]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.2+
callables_immediately_invoked_closure.php:31: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing(). [reported-from-level=2]
callables_immediately_invoked_closure.php:31: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
callables_immediately_invoked_closure.php:39: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing(). [reported-from-level=2]
callables_immediately_invoked_closure.php:39: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
callables_immediately_invoked_closure.php:47: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing(). [reported-from-level=2]
callables_immediately_invoked_closure.php:47: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
callables_immediately_invoked_closure.php:31: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:31: Parameter #1 (mixed) of echo cannot be converted to string.
callables_immediately_invoked_closure.php:39: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
callables_immediately_invoked_closure.php:47: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass
callables_immediately_invoked_closure.php:31: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:31: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]
callables_immediately_invoked_closure.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:39: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]
callables_immediately_invoked_closure.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:47: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]
mago 1.57.0mago 1.57.0 Pass
callables_immediately_invoked_closure.php:31: Method `missing` does not exist on type `Conformance\Tests\CallablesImmediatelyInvokedClosure\User`. [non-existent-method]
callables_immediately_invoked_closure.php:31: The first value for `echo` is too general. [mixed-argument]
callables_immediately_invoked_closure.php:39: Method `missing` does not exist on type `Conformance\Tests\CallablesImmediatelyInvokedClosure\User`. [non-existent-method]
callables_immediately_invoked_closure.php:39: The first value for `echo` is too general. [mixed-argument]
callables_immediately_invoked_closure.php:47: Method `missing` does not exist on type `Conformance\Tests\CallablesImmediatelyInvokedClosure\User`. [non-existent-method]
callables_immediately_invoked_closure.php:47: The first value for `echo` is too general. [mixed-argument]
mir 0.87.0mir 0.87.0 Pass
callables_immediately_invoked_closure.php:31: UndefinedMethod: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing() does not exist [MIR0004]
callables_immediately_invoked_closure.php:39: UndefinedMethod: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing() does not exist [MIR0004]
callables_immediately_invoked_closure.php:47: UndefinedMethod: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing() does not exist [MIR0004]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
callables_immediately_invoked_closure.php:31: Method 'missing' not found on class 'Conformance\Tests\CallablesImmediatelyInvokedClosure\User' [unknown_member]
callables_immediately_invoked_closure.php:39: Method 'missing' not found on class 'Conformance\Tests\CallablesImmediatelyInvokedClosure\User' [unknown_member]
callables_immediately_invoked_closure.php:47: Method 'missing' not found on class 'Conformance\Tests\CallablesImmediatelyInvokedClosure\User' [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Pass
callables_immediately_invoked_closure.php:31: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
callables_immediately_invoked_closure.php:31: Undefined method 'missing'. [P1013]
callables_immediately_invoked_closure.php:39: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
callables_immediately_invoked_closure.php:39: Undefined method 'missing'. [P1013]
callables_immediately_invoked_closure.php:47: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
callables_immediately_invoked_closure.php:47: Undefined method 'missing'. [P1013]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail
callables_immediately_invoked_closure.php:31: Method "missing" does not exist on class "Conformance\Tests\CallablesImmediatelyInvokedClosure\User" [missing_member]
Expectation diff
Line 39: Expected 1 error(s)
Line 47: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Pass
callables_immediately_invoked_closure.php:31: Call to unknown method: Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing()
callables_immediately_invoked_closure.php:39: Call to unknown method: Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing()
callables_immediately_invoked_closure.php:47: Call to unknown method: Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing()
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 31: Expected 1 error(s)
Line 39: Expected 1 error(s)
Line 47: 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

No diagnostics reported.

Expectation diff
Line 31: Expected 1 error(s)
Line 39: Expected 1 error(s)
Line 47: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 31: Expected 1 error(s)
Line 39: Expected 1 error(s)
Line 47: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
callables_immediately_invoked_closure.php:31: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:31: Parameter #1 (mixed) of echo cannot be converted to string.
callables_immediately_invoked_closure.php:39: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
callables_immediately_invoked_closure.php:47: Call to an undefined method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing().
callables_immediately_invoked_closure.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
callables_immediately_invoked_closure.php:31: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:31: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]
callables_immediately_invoked_closure.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:39: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]
callables_immediately_invoked_closure.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
callables_immediately_invoked_closure.php:47: Method Conformance\Tests\CallablesImmediatelyInvokedClosure\User::missing does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\CallablesImmediatelyInvokedClosure;

/**
 * Return type of an immediately invoked closure.
 *
 * An immediately invoked closure or arrow function, `(fn (): T => …)()`,
 * evaluates to the closure's return type. The control assigns the closure
 * first and calls the variable, which most tools already handle.
 *
 * References:
 * - https://www.php.net/manual/en/functions.arrow.php
 */

final class User
{
    public function name(): string
    {
        return 'user';
    }
}

function assignedThenCalled(): void
{
    $make = fn (): User => new User();
    echo $make()->name(); // V
    $build = fn (): User => new User();
    echo $build()->missing(); // E: the call returns a User
}

function invokedArrow(): void
{
    $user = (fn (): User => new User())();
    echo $user->name(); // V
    $made = (fn (): User => new User())();
    echo $made->missing(); // E: the call returns a User
}

function invokedClosure(): void
{
    $built = (function (): User {
        return new User();
    })();
    echo $built->missing(); // E: the call returns a User
}