phpsymfonytwigpsalm-php

Psalm error for TwigFilter callable parameter


I get this error from psalm for a twig extension:

ERROR: InvalidArgument - src/Twig/CartExtension.php:44:17 - Argument 2 of Twig\TwigFilter::__construct expects callable|null, array{App\Twig\CartExtension&static, string(getOfferDate)} provided

The code in question is:

public function getFilters(): array
{
    return [
        new TwigFilter(
            'get_offer_date',
            [$this, 'getOfferDate']
        ),
    ];
}

And the TwigFilter signature is

/**
 * @param callable|null $callable A callable implementing the filter. If null, you need to overwrite the "node_class" option to customize compilation.
 */
public function __construct(string $name, $callable = null, array $options = [])

...

The twig docs recommend this format

[$this, 'getOfferDate']

for the callable: https://symfony.com/doc/current/templating/twig_extension.html

How can i fix this psalm error or tell psalm to accept it?


Solution

  • Method getOfferDate() should be created inside of class. In your case, inside of class CartExtension.