Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc-blocks for inferred return type of arrow functions #4209

Closed
alecwcp opened this issue Sep 18, 2020 · 2 comments
Closed

Doc-blocks for inferred return type of arrow functions #4209

alecwcp opened this issue Sep 18, 2020 · 2 comments

Comments

@alecwcp
Copy link

alecwcp commented Sep 18, 2020

Since the body of arrow functions is a single statement, when psalm is unable to infer the return type there isn't an immediate place to put the annotation needed.

A contrived but demonstrating example is https://psalm.dev/r/926254686a

If the body of the function is made an assignment (since assignment returns the value too) an annotation can then be added that works, e.g.

$b = array_filter(
    $methodCalls,
    fn (string $methodCall): bool =>
        /** @var bool */
    	$dummy = $a->{$methodCall}
);

but this has forced us to introduce an unused variable.

This works because of the support documented here https://psalm.dev/docs/annotating_code/supported_annotations/ under "Off-label usage of the @var tag", specifically

If no VariableReference is given, the annotation tells Psalm that the right hand side of the expression, whether an assignment or a return, is of type Type.

Is it possible to expand on that (or another annotation?) to allow doing

$b = array_filter(
    $methodCalls,
    fn (string $methodCall): bool =>
        /** @var bool */
    	$a->{$methodCall}
);

and so removing the redundant variable?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/926254686a
<?php
  
class A {
	public function __call(string $methodName, array $methodArguments)
    {
        if (substr($methodName, 0, 5) === 'isset') {
            return true;
        }
        return reset($methodArguments);
    }
}

$a = new A();

$methodCalls = ['isset1', 'isset2', 'isset3'];

$b = array_filter(
    $methodCalls,
    fn (string $methodCall): bool =>
    	$a->{$methodCall}
);

echo count($b);
Psalm output (using commit 5db75df):

INFO: MixedReturnStatement - 20:6 - Could not infer a return type

INFO: MixedInferredReturnType - 19:30 - Could not verify return type 'bool' for /var/www/vhosts/psalm.dev/httpdocs/src/somefile.php:19:334:-:closure

@weirdan weirdan added the bug label Sep 18, 2020
@muglug muglug added enhancement and removed bug labels Sep 19, 2020
@muglug muglug closed this as completed in f973937 Sep 19, 2020
@alecwcp
Copy link
Author

alecwcp commented Sep 20, 2020

Sorry to comment on a closed issue but...

Just wanted to say that the response times of you guys are amazing 💯 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants