0

How to configure PhpStorm for visualizing available methods and attributes of an object variable while typing?

I remembered having seeing that adding the @var annotations on the line above of the declared variable as

// @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $entityAdapter 
$entityAdapter = $entityReference->getTarget();

permits it but I do not know what does it do and how to use it.

An example of autocompletion: annotating a local variable in php

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Matoeil
  • 6,851
  • 11
  • 54
  • 77
  • 1
    Sadly it's not clear to me at all what you actually want here. Try re-wording your description / provide details of the actual issue that you are having. – LazyOne Dec 01 '20 at 17:20
  • 1
    `/** @var \App\MyCLass $myVar */` -- this is PHPDoc for inline variable. It provides a type hint for IDE: it tells that `$myVar` variable is an instance of `\App\MyClass` .. so that IDE can use that info for its needs (e.g. for code completion or static analysis). It is used in cases of dynamic code where IDE (using static analysis) cannot say what the variable is/what that methods returns. Very often happens when return type depends on the passed parameter or on a type/value of some class property. I find that the link you have got there describes it quite well. – LazyOne Dec 01 '20 at 18:24
  • 1
    You may check PHPDoc fig proposals to possible have some better ideas: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#517-var etc – LazyOne Dec 01 '20 at 18:25
  • 1
    Using you code example, it basically tells (better say "should tell" as after your edit it is no longer a valid PHPDoc) that `$entityAdapter` variable is an instance of `\Drupal\Core\Entity\Plugin\DataType\EntityAdapter` class (in case if IDE cannot figure it out due to the lack of code documentation (return types) or some specific code (e.g. very dynamic code that standard static analysis cannot handle)). Such info allow IDE to offer code completion and other stuff for that variable later in the method body. – LazyOne Dec 01 '20 at 18:30

0 Answers0