3

I am using Yii2 framework in PhpStorm.

My problem arises in views when I am using $this->render function to include another code of snippet inside my main file with some variables.

The code itself works perfectly I just have a problem with highlightings.

This is my code:

<?php
echo $this->render('commentsBlock', [
            "comments" => $comments,
            'deleteURL' => $deleteURL,
            'editURL' => $editURL,
        ]);
?>

The code above renders commentsBlock.php and content of that target file is below:

enter image description here enter image description here

As you can see the PhpStorm thinks that variables are not declared when they are defined.

I know that I need to add some comment which tells the IDE that vars do exist but what I tried did not work so far.

I did this:

enter image description here

But it is not highlighting.

Any ideas on how to write properly this comment section to trick IDE to highlight my variables?

n1md7
  • 2,854
  • 1
  • 12
  • 27
  • 3
    Try to use a double asterisk (`/** @var array $comments */`). Or maybe `@global` annotation. – Michał Haracewiat Jul 02 '19 at 08:07
  • Related https://stackoverflow.com/questions/11751330/phpstorm-warning-php-variable-might-not-have-been-defined https://stackoverflow.com/q/42837882/2943403 – mickmackusa Jul 02 '19 at 08:07
  • @mickmackusa I'm not giving him solutions but asking to check something. Once Harry confirms this solves his problem I'll post it as a solution. I don't want to post anything I'm not certain about. – Michał Haracewiat Jul 02 '19 at 08:10
  • Your wording doesn't suggest that you are seeking a response. – mickmackusa Jul 02 '19 at 08:11
  • It has been solved. It works with single asterisk comment like mentioned *Bizley* below. And works with double one too. @Haru `/** @var array $comments */` both are valid on my IDE – n1md7 Jul 02 '19 at 08:17
  • 1
    @harry do you necessarily need to declare the variable type? Does this work too? (I'm not at my computer to test) https://stackoverflow.com/a/31730971/2943403 – mickmackusa Jul 02 '19 at 08:58
  • @mickmackusa Nope, I tested it and the type is like an optional parameter. Just `/* @var $myVar */` is totally enough. But `/* @var $myVar string */` is valid too – n1md7 Jul 02 '19 at 09:03

1 Answers1

9

Use vardoc like this:

/* @var $comments array */

This syntax is a bit different from phpdoc standard (notice the variable name and type switched places) but it's widely recognised by IDEs (including PhpStorm).

For more references see this answer for example.

Bizley
  • 17,392
  • 5
  • 49
  • 59