Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm property annotation for instance of class

I know I can use PhpStorm annotations like this:

/**
 * Class Model
 * @property string name
 */
class Model {};

$modelInstance = new Model();
$modelInstance->name;

When I type $modelInstance-> PhpStorm will offer me "name" in autocomplete.

Is it possible to create custom property annotations for instances of classes?

/**
 * Class Model
 * @property string name
 */
class Model {};

/**
 * @var Model $modelInstance @property text
 */
$modelInstance = new Model();
$modelInstance->text; //PhpStorm does not know about this property

I would like to have property "text" in PhpStorm autocomplete but ONLY for $modelInstance. Not for every instance of class Model.

like image 665
Michal Avatar asked Jul 29 '16 10:07

Michal


1 Answers

This is my solution based on LazyOne's opinion.

/**
 *
 * Class ZboziDibiRow
 * @property int id
 * @property string name
 * @property string store
 * @property string uri
 * @property string manufacturer
 * @property string description
 * @property int price
 * @property string ean
 * @property string code
 * @property int available_in
 * @property string zbozi_category_id
 * @property string category_recursive_id
 */
class ZboziDibiRow extends DibiRow
{
}

Now when I have something like:

/**
* @var ZboziDibiRow[]
*/
public $products;

I will get autocomplete for:

$zbozi = new Zbozi();
foreach ($zbozi->products as $key => $product) {
    $product-> ....
like image 167
Michal Avatar answered Sep 22 '22 11:09

Michal