【发布时间】:2016-04-20 11:58:21
【问题描述】:
问题
是否可以使用模型/存储库模式在 PhpStorm 中完成行代码?
我的设置
我正在使用 Laravel 并按照 Laracasts 视频中的描述实现存储库模式。
守则
这是一个基本示例,说明模型如何在 Laravel 中工作,以及如何让模型属性的代码完成似乎是不可能的。
此代码正常工作并打印出“billy”,但属性 $name 没有类型提示,也不会在 PhpStorm 中完成代码。类型提示是优先考虑父属性定义类型而不是子属性,这对我来说似乎很奇怪。
<?php
// Models
abstract class Model {
public $sqlTableName;
public function findFromDatabase($id)
{
$model = new $this;
// This would be grabbed using table name and $id
$fakeDatabaseRow = ['name' => 'billy', 'job' => 'engineer'];
foreach ($fakeDatabaseRow as $column => $value) {
$model->$column = $value;
}
return $model;
}
}
class User extends Model {
public $name;
public $job;
public $sqlTableName = 'users';
}
// Repositories
abstract class RepositoryBase {
/**
* @var Model
*/
public $model;
public function find($id)
{
$this->model = $this->model->findFromDatabase(1);
return $this->model;
}
}
class UserRepository extends RepositoryBase {
/**
* @var User
*/
public $model;
public function __construct(User $model)
{
$this->model = $model;
}
}
// Run
$model = new User();
$userRepository = new UserRepository($model);
echo $userRepository->find(1)->name;
一个丑陋的修复
真正获得代码完成的唯一方法似乎是使用新的 php doc 块重新定义子函数:
class UserRepository extends RepositoryBase {
/**
* @var User
*/
public $model;
public function __construct(User $model)
{
$this->model = $model;
}
// I need to replace this function for every different repository
// even though they are all the same
/**
* @param $id
* @return User
*/
public function find($id)
{
return parent::find($id);
}
}
但是,我有数百个模型、存储库和存储库函数。重写每个实现中的所有函数将是一项艰巨的工作。
有没有办法让 PhpStorm 使用孩子的类型提示声明而不是父母,而无需重新声明该方法?
【问题讨论】:
-
在 PHPDoc 中尝试
@method类。 -
@LazyOne - 如果我将
@method User find($id)添加到 UserRepository 文档块,那确实有效。然而,这仍然意味着我需要为每个存储库和每个返回模型的函数执行此操作。 -
` ¯\_(ツ)_/¯ ` 1) 至少你不要重新声明它 2) 我不知道其他方法 3) 也许 Laravel 支持插件可以做到这一点(如果你还没有安装)
标签: php laravel phpstorm repository-pattern type-hinting