如何让PhpStorm识别抽象仓库类loadById返回的具体实体类型?
问题描述
在我们的应用里,我们用仓库类处理从数据库获取的模型。我们定义了抽象仓库类EntityRepository,它封装了数据库操作能力,包含loadById方法用于加载指定ID的数据库记录,还有抽象方法getEntity用来创建对应仓库的实体对象。相关代码如下:
abstract class EntityRepository { /** * @param int $id * @return AbstractEntity */ public function loadById($id) { $record = $this->db->loadById($id); $entity = $this->getEntity(); return $this->inflate($record, $entity); } /** * @return AbstractEntity */ protected abstract function getEntity(); } class PeopleRepository extends EntityRepository { protected function getEntity() { return new PeopleEntity(); } } abstract class AbstractEntity { private $id; /** * @return int */ public function getId() { return $this->id; } /** * @param int $id; */ public function setId($id) { $this->id = $id; } } class PeopleEntity extends AbstractEntity { private $name; /** * @return string */ public function getName() { return $this->name; } /** * @param string $name; */ public function setName($name) { $this->name= $name; } }
现在遇到的问题是:当用PeopleRepository实例调用loadById方法时,PhpStorm只能识别返回值为AbstractEntity,只能提供父类的方法补全,没法识别具体的PeopleEntity类型。我们不想逐个枚举所有仓库类来配置元数据,希望通过抽象层面的定义,让所有EntityRepository的子类调用loadById时,都能返回对应getEntity()定义的实体类型。
解决方案
你可以用PhpStorm支持的**PHP模板类型(@template)**来实现这个需求,完全不需要逐个配置每个仓库类,只需要修改抽象仓库类的注解即可:
- 在抽象
EntityRepository类上添加@template注解,定义一个代表实体类型的参数(比如TEntity),并限制它必须是AbstractEntity的子类; - 把
getEntity方法的返回类型改成这个模板参数TEntity,明确它返回的是当前仓库对应的具体实体; - 把
loadById方法的返回类型也改成TEntity,让IDE知道这个方法返回的就是对应仓库的实体类型。
修改后的抽象仓库类代码如下:
/** * @template TEntity of AbstractEntity */ abstract class EntityRepository { /** * @param int $id * @return TEntity */ public function loadById($id) { $record = $this->db->loadById($id); $entity = $this->getEntity(); return $this->inflate($record, $entity); } /** * @return TEntity */ protected abstract function getEntity(); }
这样修改之后,PeopleRepository不需要任何额外注解——PhpStorm会自动根据getEntity返回的PeopleEntity推断出TEntity的具体类型。现在你调用$peopleRepo->loadById(1)时,IDE会正确识别返回值是PeopleEntity,并给出getName()、setName()这些子类方法的代码补全,完美解决类型推断的问题。
内容的提问来源于stack exchange,提问作者Nico Haase
相关产品推荐
相关产品推荐

