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

[Enhancement] Emit an error when phpdoc is impossible #5990

Closed
mr-feek opened this issue Jun 24, 2021 · 7 comments
Closed

[Enhancement] Emit an error when phpdoc is impossible #5990

mr-feek opened this issue Jun 24, 2021 · 7 comments

Comments

@mr-feek
Copy link
Contributor

mr-feek commented Jun 24, 2021

https://psalm.dev/r/af5065a4c0

Given that bar has a native return type of string, shouldn't psalm be able to emit an error that the phpdoc is invalid? I'm thinking that phpdoc should only become the source of truth if there are no native typehints or the typehints are castable to each other..

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/af5065a4c0
<?php

/**
* @method array bar()
*/
interface Foo
{
    public function bar(): string;
}

function test(Foo $foo): string
{
    return $foo->bar();
}
Psalm output (using commit 38d3b15):

ERROR: InvalidReturnStatement - 13:12 - The inferred type 'array<array-key, mixed>' does not match the declared return type 'string' for test

ERROR: InvalidReturnType - 11:26 - The declared return type 'string' for test is incorrect, got 'array<array-key, mixed>'

@weirdan
Copy link
Collaborator

weirdan commented Jun 24, 2021

I think it should be consistent with @return: https://psalm.dev/r/cba0b4b5a3, i.e. trust the docblock but emit MismatchingDocblock issue

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/cba0b4b5a3
<?php

/**
* @method array bar()
*/
interface Foo
{
    public function bar(): string;
    
    /** @return array */
    public function baz(): string;
}

function test(Foo $foo): string
{
    if (rand(0, 1)) {
        return $foo->bar();
    } else {
        return $foo->baz();
    }
}
Psalm output (using commit 38d3b15):

ERROR: InvalidReturnStatement - 17:16 - The inferred type 'array<array-key, mixed>' does not match the declared return type 'string' for test

ERROR: InvalidReturnStatement - 19:16 - The inferred type 'array<array-key, mixed>' does not match the declared return type 'string' for test

ERROR: InvalidReturnType - 14:26 - The declared return type 'string' for test is incorrect, got 'array<array-key, mixed>'

ERROR: MismatchingDocblockReturnType - 10:17 - Docblock has incorrect return type 'array<array-key, mixed>', should be 'string'

@orklah
Copy link
Collaborator

orklah commented Jun 24, 2021

I think this is a duplicate of #5786

@weirdan
Copy link
Collaborator

weirdan commented Jun 24, 2021

We don't have any inheritance here, so the only possible developer intent is to clarify native return type. This makes it very clear that contradiction is a different problem to LSP violations that linked ticket discusses.

robchett added a commit to robchett/psalm that referenced this issue Nov 9, 2023
robchett added a commit to robchett/psalm that referenced this issue Nov 9, 2023
This was referenced Feb 9, 2024
@orklah
Copy link
Collaborator

orklah commented Mar 11, 2025

https://psalm.dev/r/af5065a4c0 emits a MismatchingDocblockReturnType now, so it should be okay now

@orklah orklah closed this as completed Mar 11, 2025
Copy link

I found these snippets:

https://psalm.dev/r/af5065a4c0
<?php

/**
* @method array bar()
*/
interface Foo
{
    public function bar(): string;
}

function test(Foo $foo): string
{
    return $foo->bar();
}
Psalm output (using commit be92afa):

ERROR: InvalidReturnStatement - 13:12 - The inferred type 'array<array-key, mixed>' does not match the declared return type 'string' for test

ERROR: InvalidReturnType - 11:26 - The declared return type 'string' for test is incorrect, got 'array<array-key, mixed>'

ERROR: MismatchingDocblockReturnType - 4:11 - The inherited return type 'string' for Foo::bar is different to the corresponding @method annotation 'array<array-key, mixed>'

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

Successfully merging a pull request may close this issue.

3 participants