PHPStorm can't find var definition, then Cmd+Click goes to unrelated file

So I notice the red underline on the var, and hover over it:

Cmd+clicking the var takes me to some file that does define it, but the file is not run in my test of the homepage even though this file is.

So, I have the Symfony plugin installed and enabled, but PHPStorm is just being scatterbrained about this var.

How do I get PHPStorm to (1) not give me contradictory info about it not finding a definition but then taking me to a definition, (2) not take me to a definition that isn't used?

I mean, it's one thing to take me to the wrong definition, but another to tell me there's no found definition when it finds a definition.

8 comments
Comment actions Permalink

Please try to enable the "Search for variable's definition outside the current file" option in "Settings/Preferences | Editor | Inspections  > PHP > Undefined symbols > Undefined variable":

Would the inspection warning still appear?

0
Comment actions Permalink

I enabled it, but since I've seen PHPStorm send me to var definitions that were not actually part of the include stream, could this setting allow for false positives? I'm concerned about it taking me to var definitions that aren't actually included (I don't know if that's completely avoidable though). Can you clue me in on this?

I mean, there's a reason JetBrains has the option disabled by default; can you tell me what the reason/s is/are?

0
Comment actions Permalink

I'd suggest telling PhpStorm explicitly:

/** @var Whatever\Actual\Type $container */

These annotations are pretty much standard and many other PHP tools (editors, linters...) will use them as well.

It also helps to keep track of what external variables your include files expect.

0
Comment actions Permalink

> IDE does not follow include/require statements

I'm not seeing an explanation of why anywhere.

> please check this ticket

I guess I sort of see an explanation here...?

"The current level of dynamic include analysis support does not let us to be quite sure on what is included from where.
We will introduce the "warning" inspection at some point and then work on the confidence."

There's a lot of posts on that ticket though, and I'm finding it difficult to tell if that quote is still the case, or what the deal is now.

I'm not even sure if that explains why PHPStorm apparently can't figure out where a variable is coming from in a lot of cases... is that quote the explanation (which isn't much of an explanation), or can you point out or explain something more telling? Is it hard to explain? Is it hard to make an editor find where a var is coming from based on execution logic (or too performance-prohibitive)?

Maybe I already have seen the answers but in this particular case, it seems like this should be easy to find because it's probably a well-known Symfony var that should be easy to find (unless maybe it's coming vendor, would that be why?). I don't know what to say about this now.

0
Comment actions Permalink

Unfortunately current behavior isn't documented or described in details anywhere. This is partially due to the fact that current behavior depends on how both inspection (undefined variable) & how resolve work.

The issue will be worked on further: https://youtrack.jetbrains.com/issue/WI-52783, https://youtrack.jetbrains.com/issue/WI-59449

0
Comment actions Permalink

OK but it would be nice to know your summation of how its logic is if you can... I'll probably have to take an hour or two out to try to understand it from all of the comments myself, and I was hoping you'd understand it better or be able to.

0
Comment actions Permalink

I'm not a developer, so please take into account that I'm not reading the source code, but rather projecting my understanding of the current behavior.

1) PhpStorm doesn't take into account any include or require statements when analyzing where the variable could possibly come from. I can't advise on reasons of that, but I would assume it's performance-related. Note that we, as users, might think that the implementation of such a resolve mechanism might be simple, but in fact it could be tied to other resolve engine aspects, making this a non-trivial task.

2) With this option enabled, PhpStorm will try to find a variable definition outside of current file: 

As far as I can see, IDE would find a random or the last-changed var definition and stick to it, in terms of resolve.

3) If the above-mentioned option is disabled - PhpStorm would look for the var definition inside the current file only. If none found - IDE would report the var as undefined. Also note that the behavior could be affected by minor bugs such as this one.

 

Let me know if I've missed something.

1

Please sign in to leave a comment.