【发布时间】:2017-02-06 23:52:18
【问题描述】:
所以我有一个 trait,如下所示:
trait RepositoryTrait
{
/**
* Find a model by its primary key.
*
* @param int $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function find($id, array $columns = ['*'])
{
return $this->query()->find($id, $columns);
}
}
和界面:
interface Repository
{
/**
* Find a model by its primary key.
*
* @param int $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function find($id, array $columns = ['*']);
}
我有类似的存储库类:
class FixedAssetRepository implements Repository
{
use RepositoryTrait;
/**
* FixedAsset model.
*
* @var FixedAsset
*/
protected $model;
/**
* Repository constructor.
*
* @param FixedAsset $fixedAsset
*/
public function __construct(FixedAsset $fixedAsset)
{
$this->model = $fixedAsset;
}
}
而我正在寻找的是以某种方式定义方法 find(在 trait 或接口中)是 FixedAsset 的类型 - 但这应该始终适用于我将创建的每个新 Repository 类(实现了 Repository 接口)。
我需要它在 PhpStorm 10.0.4
中进行类型提示有可能吗?
所以结果应该是当我在某处打电话时,即:
$fixedAsset = $this->fixedAssetRepositry->find($id);
PhpStorm 会知道 $fixedAsset 是类 FixedAsset 的对象,而不仅仅是 \Illuminate\Database\Eloquent\Model(现在就是这样工作的)。
所以我需要类似界面(或特征?)的东西:
/**
* Find a model by its primary key.
*
* @param int $id
* @param array $columns
* @return $this->model
*/
public function find($id, array $columns = ['*']);
当然@return $this->model 不起作用。
如有任何建议,我们将不胜感激。
【问题讨论】:
-
我现在能想到的最好的方法是使用
@method并以正确的返回类型“重新声明”它——看看这是否可行。 -
太棒了!多么简单……这就是我需要的。非常感谢。您可以发布此答案,我可以将其标记为解决方案。