【问题标题】:PHPStan with Doctrine and find() - Unable to resolve the template type T带有 Doctrine 和 find() 的 PHPStan - 无法解析模板类型 T
【发布时间】:2021-07-23 03:14:44
【问题描述】:

我有这个特殊的代码:

<?php declare(strict_types = 1);

interface ObjectManager
{
    /**
     * @param string $className The class name of the object to find.
     * @param mixed  $id        The identity of the object to find.
     * @psalm-param class-string<T> $className
     *
     * @return object|null The found object.
     * @psalm-return T|null
     *
     * @template T of object
     */
    public function find($className, $id);
}

abstract class AbstractAppEntityRepository
{
    protected ObjectManager $manager;

    public function __construct(ObjectManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * @template T
     * @param class-string<T> $className
     * @param int|string|array<string, string|int> $id
     * @return T|null
     */
    protected function findEntity(string $className, array | int | string $id)
    {
        /**
         * @var T|null $entity
         * @phpstan-var T|null $entity
         */
        $entity = $this->manager->find($className, $id);

        return $entity;
    }
}

PHPStan 游乐场在分析后给我一个错误:

第 39 行 - 在调用方法 ObjectManager::find() 时无法解析模板类型 T

游乐场链接:LINK

需要一些帮助,因为我不知道如何管理。提前感谢您的帮助!

【问题讨论】:

    标签: php doctrine phpstan


    【解决方案1】:

    将类型变量声明为 @template T of object: https://phpstan.org/r/587e228b-0c21-4def-bfed-4cd53b955a4b 会有所帮助

    内联 @phpstan-var 也不是必需的。

    【讨论】:

    • 谢谢 Ondřej!就是这样! :)
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多