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?

7
  • 4
    Try to use a double asterisk (/** @var array $comments */). Or maybe @global annotation. Commented Jul 2, 2019 at 8:07
  • Related stackoverflow.com/questions/11751330/… stackoverflow.com/q/42837882/2943403 Commented Jul 2, 2019 at 8: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. Commented Jul 2, 2019 at 8:10
  • Your wording doesn't suggest that you are seeking a response. Commented Jul 2, 2019 at 8:11
  • 1
    @harry do you necessarily need to declare the variable type? Does this work too? (I'm not at my computer to test) stackoverflow.com/a/31730971/2943403 Commented Jul 2, 2019 at 8:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't that /** @var $comments array */ instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.