带有 Doctrine 和 find() 的 PHPStan - 无法解析模板类型 T
PHPStan with Doctrine and find() - Unable to resolve the template type T
我有这个特定的代码:
<?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 游乐场在分析后给我一个错误:
Line 39 - Unable to resolve the template type T in call to method ObjectManager::find()
Link去游乐场:LINK
需要一些帮助,因为我不知道如何处理。预先感谢您的帮助!
将类型变量声明为 @template T of object
会有所帮助:https://phpstan.org/r/587e228b-0c21-4def-bfed-4cd53b955a4b
内联 @phpstan-var
也不是必需的。
我有这个特定的代码:
<?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 游乐场在分析后给我一个错误:
Line 39 - Unable to resolve the template type T in call to method ObjectManager::find()
Link去游乐场:LINK
需要一些帮助,因为我不知道如何处理。预先感谢您的帮助!
将类型变量声明为 @template T of object
会有所帮助:https://phpstan.org/r/587e228b-0c21-4def-bfed-4cd53b955a4b
内联 @phpstan-var
也不是必需的。