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

Prevent intersections between impossible interface methods #1690

Open
bdsl opened this issue May 26, 2019 · 3 comments
Open

Prevent intersections between impossible interface methods #1690

bdsl opened this issue May 26, 2019 · 3 comments

Comments

@bdsl
Copy link
Contributor

bdsl commented May 26, 2019

As metnioned in comments of #1686. Code at https://psalm.dev/r/20aa96387e :

<?php

interface IFoo {
    function foo() : string;
}

interface IBar {
    function foo() : int;
}

/** @param IFoo&IBar $i */
function iFooFirst1($i) : int {
    return $i->foo();
}

/** @param IFoo&IBar $i */
function iFooFirst2($i) : string {
    return $i->foo();
}
Psalm output (using commit d51ee03):

ERROR: InvalidReturnStatement - 18:12 - The type 'int' does not match the declared return type 'string' for iFooFirst2

ERROR: InvalidReturnType - 17:27 - The declared return type 'string' for iFooFirst2 is incorrect, got 'int'

Psalm reports issues about iFooFirst2, but nothing about iFooFirst1. There should be issues reported for both iFooFirst1 and iFooFirst2, since no class can implement both interfaces. If the interface types were docbloc types then the class could exist, but it's foo function wouldn't be able return without violating LSP, so again anything expecting a return value from it is likely wrong.

@muglug
Copy link
Collaborator

muglug commented May 27, 2019

I think Psalm should just prevent the intersection in the first place.

@muglug muglug changed the title intersection between int and string should return no-return Prevent intersections between impossible interface methods May 27, 2019
@AndrolGenhald
Copy link
Collaborator

This is improved now to show mixed as the return type since the interfaces don't agree, but it would still be nice to show a warning for the impossible intersection as well. I've added a test for this to my type comparison rewrite.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7d06648934
<?php

interface IFoo {
    function foo(): string;
}

interface IBar {
    function foo(): int;
}

/** @param IFoo&IBar $i */
function iFooFirst1($i): mixed {
    return $i->foo();
}
Psalm output (using commit 2e2099e):

No issues!

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