psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Psalm don't keep callable type in union types after condition

Open vjik opened this issue 1 year ago • 1 comments

See https://psalm.dev/r/393eb25a8a

vjik avatar Aug 03 '24 13:08 vjik

I found these snippets:

https://psalm.dev/r/393eb25a8a
<?php

/**
 * @psalm-param array|callable():array $var
 */
function text(mixed $var): array
{   
    if (is_array($var)) {
        return $var;
    }

    /** @psalm-trace $var */

    return call_user_func($var);
}
Psalm output (using commit 16b24bd):

INFO: MixedReturnStatement - 14:12 - Could not infer a return type

INFO: Trace - 14:5 - $var: callable-object|callable-string

psalm-github-bot[bot] avatar Aug 03 '24 13:08 psalm-github-bot[bot]

~Same behavior with Closure: https://psalm.dev/r/0f016891bd~

vjik avatar Mar 10 '25 06:03 vjik

I found these snippets:

https://psalm.dev/r/0f016891bd
<?php

/**
 * @psalm-param array|Clsoure():array $var
 */
function text(mixed $var): array
{   
    if (is_array($var)) {
        return $var;
    }

    /** @psalm-trace $var */

    return $var();
}
Psalm output (using commit be92afa):

ERROR: InvalidDocblock - 4:17 - Parenthesis must be preceded by “Closure”, “callable”, "pure-callable" or a valid @method name in docblock for text

INFO: MixedFunctionCall - 14:12 - Cannot call function on mixed

INFO: MixedReturnStatement - 14:12 - Could not infer a return type

INFO: Trace - 14:5 - $var: mixed

psalm-github-bot[bot] avatar Mar 10 '25 06:03 psalm-github-bot[bot]

Are you sure it's not due to the typo in Closure in your docblock?

orklah avatar Mar 10 '25 07:03 orklah

Yes. Sorry, I'm wrong.

vjik avatar Mar 10 '25 07:03 vjik