← All results

Return type of an invoked first-class method callable

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

Invoking a first-class callable created from a method, $f = $object->method(...); $f(), returns the method's return type — the same as for a function-based fn(...), which is the control here. References: - https://wiki.php.net/rfc/first_class_callable_syntax

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

No diagnostics reported.

Expectation diff
Line 44: Expected 1 error(s)
Line 52: 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 44: Expected 1 error(s)
Line 52: Expected 1 error(s)
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

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

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\CallablesFirstClassMethodInvocation;

/**
 * Return type of an invoked first-class method callable.
 *
 * Invoking a first-class callable created from a method,
 * `$f = $object->method(...); $f()`, returns the method's return type —
 * the same as for a function-based `fn(...)`, which is the control here.
 *
 * References:
 * - https://wiki.php.net/rfc/first_class_callable_syntax
 */

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

final class Repository
{
    public function find(): User
    {
        return new User();
    }
}

function findUser(): User
{
    return new User();
}

function functionCallable(): void
{
    $find = findUser(...);
    echo $find()->name(); // V
    $lookup = findUser(...);
    echo $lookup()->missing(); // E: the call returns a User
}

function methodCallable(Repository $repository): void
{
    $find = $repository->find(...);
    echo $find()->name(); // V
    $lookup = $repository->find(...);
    echo $lookup()->missing(); // E: the call returns a User
}