2016-09-28 131 views
0

所以我有一個性狀像如下:PHPDoc的@返回類型等於類字段類型(在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 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類(實現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,但不起作用。

我會很感激的任何建議。

+0

最好的** **我能想到的,現在是使用'@ method'和 「重新聲明」 它正確的返回類型 - 看看這是否會工作。 – LazyOne

+0

太棒了!多麼簡單...這就是我需要的。非常感謝你。您可以發佈這個答案,我可以將其標記爲解決方案。 –

回答

1

,我能想到的,現在唯一的解決辦法是在爲FixedAssetRepository級和「重新聲明」 PHPDoc的評論使用@methodfind()方法與正確的簽名(返回類型)。由於它是一個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