6

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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Michal
  • 4,952
  • 8
  • 30
  • 63
  • 2
    Nope. `@property` can be declared in PHPDoc block for a **class only**. If you need something like that .. then the best I can suggest is to declare custom class that extends your `Model` for that. – LazyOne Jul 29 '16 at 10:56
  • I wanted to use this with DibiRow object that is returned by Dibi when selecting data from database. So that I could tell phpstorm what columns I selected - what properties are available in my instance of DibiRow. – Michal Jul 29 '16 at 11:01
  • 2
    Well ... this is for PhpStorm only ... so you can try it and see how it will do for you: 1) create your own class `MyClass extends DibiRow` and declare such custom properties there; 2) Whenever you will need to use it, use `/** @var MyClass $modelInstance */` -- this PHPDoc comment will be used by IDE only; 3) Place file(s) with such custom classes anywhere in the project (e.g. `.phpstorm` folder etc) -- it will be used by IDE only. – LazyOne Jul 29 '16 at 11:11
  • 1
    The cons of such solution is very simple -- all these extra moves just for IDE to help you with code completion. That's one of the price aspects you have to pay for using magic methods/accessing non-existing properties (one class that suits every scenario). On another hand -- using own entity class for each DB Entity is a good solution anyway. – LazyOne Jul 29 '16 at 11:12
  • That is actually pretty cool. I can accept your comment as final answer. :) – Michal Jul 29 '16 at 11:20

1 Answers1

5

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-> ....
Michal
  • 4,952
  • 8
  • 30
  • 63