Easier way to set default var if empty so PHPStorm can find its declaration?

I'm aware of a "bug" (or lack of feature) where PHPStorm has trouble zeroing in on the actual declaration of a var if it's inside of an if or else block (so that Ctrl+click any instance of the var will show all instances of the var instead of just the declaration), so I've written this to compensate for our situation:

`$config = isset($config) && is_array($config) ? $config : array();`
(not sure what happened to the code block button)
(doesn't look like markdown works anymore either)

I'm simply wondering if this is the best way to make a workaround or if this causes extra work in the PHP parser or could cause anomalous behavior with assigning $config to itself if it exists... like changing its memory reference or something else weird.

Anyway, the reason I came up with this solution is because the $config var and value can be dynamically generated by code that checks memcache... if another var determines it needs to be refreshed, it refreshes $config as well inside of if statements like:

```
if (!isset($config) || !is_array($config) {
    $config = array();
}
```
PHPStorm will show the declaration line as well as all other instances of usage of the var (very inconvenient), when with the first code statement it'll just show that declaration (very convenient).

So, main issue is if the ternary is bad (or if there's a better way), secondary issue is of course some way that isn't bad so PHPStorm isolates the declaration trace results to just the declaration(s).

4 comments

So, main issue is if the ternary is bad (or if there's a better way)

I am afraid my input won't be too valuable, but assigning a var to itself is a totally legit operation, otherwise who would use the ternary operator at all.

secondary issue is of course some way that isn't bad so PHPStorm isolates the declaration trace results to just the declaration(s).

At least, nothing known, otherwise we'd post it in the bug.

(not sure what happened to the code block button) (doesn't look like markdown works anymore either)

ZenDesk still haven't added proper formatting here (I guess it's just that there's not enough people affected. Oh, the irony).

However, it's WYSIWYG, so I ended up typing text in the GitHub Markdown editor and copying the rendered results here.

0

I've seen a lot of ternaries but hardly any that assigned a variable to itself (since the if / else case at least makes more direct logical sense), which is why I asked. I'm only instituting the ternary so PHPStorm will exclusively show it on Ctrl+click (whatever that's called, Find Declaration or something). There's other vars that are set from memcache (in another if block) so it causes the same problem.

However the problem is that PHPStorm is just not isolating the declaration if it's in an if block... it really should, I see no reason why it shouldn't.

This site used to allow code block markdown but doesn't now for some reason... Slack's or Reddit's works, I wish they'd institute something like those.

0

the problem is that PHPStorm is just not isolating the declaration if it's in an if block... it really should, I see no reason why it shouldn't.

0

Dmitry, I left two code samples in my original post...

Here's the "bug" it's based on: https://youtrack.jetbrains.com/issue/WI-34554

I'm simply looking for an

Easier way to set default var if empty so PHPStorm can find its declaration

My ternary works, but I was hoping for a better/more secure/easier (to read?) way...

So without a PHPStorm fix or adjustment, it becomes a PHP coding question.

It's OK if you're not sure.

0

Please sign in to leave a comment.