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

Incorrect RedundantCondition with class-string<Foo> and instanceof #11076

Open
jnvsor opened this issue Aug 18, 2024 · 1 comment
Open

Incorrect RedundantCondition with class-string<Foo> and instanceof #11076

jnvsor opened this issue Aug 18, 2024 · 1 comment
Labels

Comments

@jnvsor
Copy link
Contributor

jnvsor commented Aug 18, 2024

https://psalm.dev/r/38e9098a53

Basically $object instanceof $class where $object = Foo and $class = class-string<Foo> is doing the hierarchy in reverse.

In the example we can see an obvious failing if you call test(['DirectoryIterator'], new ArrayIterator([])). - both are Traversable but that doesn't mean the instanceof check is redundant because we're not checking Traversable directly.

Copy link

I found these snippets:

https://psalm.dev/r/38e9098a53
<?php

/**
 * @psalm-param class-string<Traversable>[] $matches
 */
function test(array $matches, object $input): bool {
    if (!$input instanceof Traversable) {
        return false;
    }
    
    foreach ($matches as $class) {
        if ($input instanceof $class) {
            return true;
        }
    }
    return false;
}
Psalm output (using commit 16b24bd):

ERROR: RedundantCondition - 12:13 - Type Traversable for $input is always Traversable

@weirdan weirdan added the bug label Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants