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

False positive InvalidCast when using never as template for class-string with template #8298

Closed
AndrolGenhald opened this issue Jul 21, 2022 · 4 comments

Comments

@AndrolGenhald
Copy link
Collaborator

https://psalm.dev/r/9a66468115

Probably somewhat related to #7913.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9a66468115
<?php

/**
 * @template T
 * @psalm-consistent-constructor
 */
class Foo
{
    /** @return ?T */
    public function bar()
    {
    }
}

/** @extends Foo<string> */
class Foo1 extends Foo {}

/** @extends Foo<never> */
class Foo2 extends Foo {}

/**
 * @template T
 * @template TFoo of Foo<T>
 *
 * @param class-string<TFoo> $fooClass
 * @return T
 */
function bar(string $fooClass)
{
    return (new $fooClass)->bar();
}

/** @psalm-trace $baz */
$baz = bar(Foo1::class);
/** @psalm-trace $baz */
$baz = bar(Foo2::class);
Psalm output (using commit 85fe7e8):

INFO: Trace - 34:1 - $baz: string

ERROR: InvalidCast - 36:12 - Foo2 cannot be cast to string

ERROR: NoValue - 36:1 - This function or method call never returns output

@roxblnfk
Copy link

I have the same error

https://psalm.dev/r/1cf2cacfc8

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1cf2cacfc8
<?php
/**
 * @template TClass of object
 */
interface InjectorInterface {}
interface Binder {
    /**
     * @template TClass of object
     *
     * @param class-string<TClass> $class
     * @param class-string<InjectorInterface<TClass>> $injector
     */
    public function bindInjector(string $class, string $injector): void;
}

class Bar implements Binder {    
    public function __construct(
    	private Binder $binder,
    ){}
    
    public function bindInjector(string $class, string $injector): void
    {
        $this->binder->bindInjector($class, $injector);
    }   
}
Psalm output (using commit e784128):

ERROR: InvalidCast - 23:45 - InjectorInterface<TClass:fn-binder::bindinjector as object> cannot be cast to string

@orklah
Copy link
Collaborator

orklah commented May 4, 2023

fixed in #9738 !

@orklah orklah closed this as completed May 4, 2023
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