Skip to content

Generics don't seem to work correctly within a class method #2019

@wouterj

Description

@wouterj

This snippet gives Method "specialMethod" does not exist on class "Node" on the last line (and also no completion), but the @implements generic annotation should tell Phpactor that $node is an instance of SpecificNode (both PHPStan and Psalm support this).

interface Node {}

/** @template T of Node */
interface NodeVisitor
{
    /** @param T $node */
    public function enter(Node $node): void;
}

class SpecificNode implements Node
{
    public function specialMethod(): string
    {
        return 'special';
    }
}

/** @implements NodeVisitor<SpecificNode> */
class SpecificNodeVisitor implements NodeVisitor
{
    public function enter(Node $node): void
    {
        $node->specialMethod(); // E: Method "specialMethod" does not exist on class "Node"
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions