PHPDoc @return 类型等于 class 字段类型(在 PhpStorm 10.0.4 中)

PHPDoc @return type equals class field type (in PhpStorm 10.0.4)

所以我有一个特质,如下所示:

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,例如:

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(在特征或接口中)是 FixedAsset 的类型 - 但这应该始终适用于每个新的 Repository class 我将创建(实现 Repository 接口)。

我需要它在 PhpStorm 10.0.4

中进行类型提示

有可能吗?

所以结果应该是当我调用某处时即:

$fixedAsset = $this->fixedAssetRepositry->find($id);

PhpStorm 会知道 $fixedAsset 是 class 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 不起作用。

如有任何建议,我将不胜感激。

我现在能想到的唯一解决方案是在 PHP 文档注释中使用 @method FixedAssetRepository class 和 "redeclare" find() 具有正确签名的方法(return 类型)。由于它是 PHPDoc 解决方案,因此在运行时不会对 PHP 代码产生任何影响。

示例代码:

/**
 * @method FixedAsset find(int $id, array $columns) Find a model by its primary key
 */
class FixedAssetRepository implements Repository
{
...

有关 @method 标签的更多信息 -- https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#711-method