Reputation: 9988
So I have a trait like as below:
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);
}
}
and the interface:
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 = ['*']);
}
and I have repository class like:
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;
}
}
and what I'm looking for is to define somehow that method find
(in trait or interface) is type of FixedAsset
- but this should work always for each new Repository
class that I'll create (that implements Repository interface).
I need it for type hinting in PhpStorm 10.0.4
Is it possible somehow?
So the result should be that when I call somewhere I.e.:
$fixedAsset = $this->fixedAssetRepositry->find($id);
PhpStorm will know that $fixedAsset
is a object of a class FixedAsset
not just \Illuminate\Database\Eloquent\Model
(now is working like that).
So I need something that like in interface (or trait?):
/**
* Find a model by its primary key.
*
* @param int $id
* @param array $columns
* @return $this->model
*/
public function find($id, array $columns = ['*']);
but of course @return $this->model
doesn't work.
I'll be appreciated for any suggestions about that.
Upvotes: 0
Views: 332
Reputation: 165098
The only solution that I can think of right now would be using @method
in PHPDoc comment for that FixedAssetRepository
class and "redeclare" that find()
method with correct signature (return type). Since it's a PHPDoc solution it will not have any effect on PHP code during runtime.
Sample code:
/**
* @method FixedAsset find(int $id, array $columns) Find a model by its primary key
*/
class FixedAssetRepository implements Repository
{
...
More on the @method
tag -- https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#711-method
Upvotes: 1