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

Trait template type check fails for static methods #5753

Open
AndrolGenhald opened this issue May 12, 2021 · 4 comments
Open

Trait template type check fails for static methods #5753

AndrolGenhald opened this issue May 12, 2021 · 4 comments

Comments

@AndrolGenhald
Copy link
Collaborator

https://psalm.dev/r/917c4e806c

False positive ImplementedParamTypeMismatch on baz, but bar works fine.

Probably has something to do with the way classes ignore templates for static methods, this doesn't seem to work at all. Of course the T param on Foo::baz can't be resolved and would have to be mixed since Foo isn't instantiated, but it might make sense to allow this so children can extend with tighter template restrictions and have Bar::baz take an int.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/917c4e806c
<?php
/**
 * @template T
 */
trait MyTrait {
    /** @param T $arg */
    abstract public function bar($arg): void;
    
    /** @param T $arg */
    abstract public static function baz($arg): void;
}

class Foo {
    /**
     * @use MyTrait<int>
     */
    use MyTrait;
    
    /** @param int $arg */
    public function bar($arg): void {}
    
    /** @param int $arg */
    public static function baz($arg): void {}
}
Psalm output (using commit 40bc7cf):

ERROR: ImplementedParamTypeMismatch - 23:32 - Argument 1 of Foo::baz has wrong type 'int', expecting 'T' as defined by MyTrait::baz
https://psalm.dev/r/ed6e47c8e4
<?php
/**
 * @template T
 */
abstract class Foo {
    /** @param T $arg */
    abstract public function bar($arg): void;
    
    /** @param T $arg */
    abstract public static function baz($arg): void;
}

/** @extends Foo<int> */
class Bar extends Foo {
    /** @param int $arg */
    public function bar($arg): void {}
    
    /** @param int $arg */
    public static function baz($arg): void {}
}
Psalm output (using commit 40bc7cf):

ERROR: UndefinedDocblockClass - 9:16 - Docblock-defined class, interface or enum named T does not exist

ERROR: ImplementedParamTypeMismatch - 19:32 - Argument 1 of Bar::baz has wrong type 'int', expecting 'T' as defined by Foo::baz

@Real-Artisan
Copy link

Is this still an issue?

@rzvc
Copy link

rzvc commented May 15, 2022

I'm experiencing the same issue with interfaces: https://psalm.dev/r/5bdf9334de

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/5bdf9334de
<?php

/**
 * @template T
 */
interface Foo
{
    /** @param T $data */
    public static function test($data) : void;
}
Psalm output (using commit f960d71):

ERROR: UndefinedDocblockClass - 8:16 - Docblock-defined class, interface or enum named T does not exist

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

5 participants