【问题标题】:Psalm error for TwigFilter callable parameterTwigFilter 可调用参数的诗篇错误
【发布时间】:2020-09-14 13:31:01
【问题描述】:

我从 psalm 得到这个错误,用于树枝扩展:

错误:InvalidArgument - src/Twig/CartExtension.php:44:17 - 参数 2 Twig\TwigFilter::__construct 期望可调用|null, 数组{App\Twig\CartExtension&static, string(getOfferDate)} 提供

有问题的代码是:

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

TwigFilter 签名是

/**
 * @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 = [])

...

树枝文档推荐这种格式

[$this, 'getOfferDate']

对于可调用对象:https://symfony.com/doc/current/templating/twig_extension.html

我该如何解决这个 psalm 错误或告诉 psalm 接受它?

【问题讨论】:

  • 似乎您可以抑制特定文件的某些错误。见here
  • 你有在同一个类中创建的方法getOfferDate吗?
  • @Leprechaun 这正是问题所在。随意发布这个作为答案。我会接受的。

标签: php symfony twig psalm-php


【解决方案1】:

方法getOfferDate() 应该在类内部创建。在您的情况下,在CartExtension 类内。

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 2021-11-13
    • 2021-03-05
    • 2021-12-14
    • 2021-04-11
    • 2020-10-07
    • 2021-06-10
    • 2020-05-16
    • 2022-11-16
    相关资源
    最近更新 更多