Skip to content

[TwigBridge] TranslationExtension constructor takes implementation but getter returns concrete class #37686

@dbrekelmans

Description

@dbrekelmans

Symfony version(s) affected: ^5.0

Description
Symfony\Bridge\Twig\Extension\TranslationExtension::__construct() has and optional NodeVisitorInterface as a second argument. This argument is stored in the $translationNodeVisitor property which has a getter public function getTranslationNodeVisitor(): TranslationNodeVisitor.

When constructing the TranslationExtension with any NodeVisitorInterface other than TranslationNodeVisitor, you will get a type error when calling getTranslationNodeVisitor()

How to reproduce

$translationExtension = new \Symfony\Bridge\Twig\Extension\TranslationExtension(
    null, 
    new \Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor()
);

$translationExtension->getTranslationNodeVisitor();

Possible Solution
Create a new interface that defines the public TranslationNodeVisitor methods and return that.

interface TranslationNodeVisitorInterface extends NodeVisitorInterface
{
    public function enable(): void;
    public function disable(): void;
    public function getMessages(): array;
}

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions