0

我有这个特定的代码:

<?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

会使用一些帮助,因为我不知道如何管理这个。预先感谢您的任何帮助!

4

1 回答 1

1

将您的类型变量声明为@template T of objecthttps ://phpstan.org/r/587e228b-0c21-4def-bfed-4cd53b955a4b

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

于 2021-07-22T19:14:44.983 回答