Object properties randomly coloured as unused

I've noticed this problem in many cases in PHP files - PhpStorm sometimes uses some weird algorithm for detecting and marking unused object properties and the results become meaningless. For example:

As you can see some properties look normal while some are in grey colour meaning "Unused element: ...". The problem is that none of these properties are used anywhere, this is a new class not yet used in the project. Then why are some properties marked as used and others not? My guess is that names like name, city, postcode and street might be the names of used properties in other classes. Trying to find usages on a supposedly used properties gives this result:

This is not a critical feature for me but to be honest I'd prefer PhpStorm not to mark any properties as unused instead of trying to do it in such an erroneous way because this ends up in a complete mess. Is there any way to remedy this?

5 comments
Comment actions Permalink

It may sound way too generic but I was wondering if you have already tried to perform the Invalidate Caches operation.

Just in case, here is the link:
https://www.jetbrains.com/help/phpstorm/invalidate-caches.html

0
Comment actions Permalink

I've just tried invalidating caches and nothing is changed. I can't figure out the pattern how PhpStorm determines what to mark as used or unused. I thought that maybe PhpStorm doesn't distinguish between different classes having properties with the same name. My general observation is that if a property name is "popular" in the project and exists in a few places as a property name or other symbol name (like a key of an array) then it is more likely to be marked as used. But this is not always so because there are a few properties whose names are so unique that they don't exist anywhere in the project apart from one declaration in a class and stll they are marked as used.

The problem is most apparent in classes with public properties. I have many simple entity classes like this and colouring of their properties seems completely random and cannot be trusted. However, it stays the same whenever I open the files so there must be some incomprehensible logic behind this.

0
Comment actions Permalink

I am not sure about the certain criteria, but in general, it's done this way for the sake of performance - if a symbol's name is short, it costs more to find its usages on the fly.
What if you add several characters to a name of a symbol that's affected by this issue, will this make the inspection kick in?

0
Comment actions Permalink

I'm not sure if the length is the criteria, when I add more characters to a property name then usually the inspection kicks in but not always, sometimes it depends on what characters I add and sometimes it kicks in when I delete characters. For example, I have this unused symbol that appears as used:

    public $operator;

Changing the name 'operator' to the following ones causes the inspection to kick in (it will be greyed out as unused):

operator1
operatorb
operatorf
operatorh
operatorj
operatork
operato

But for these names it will not:

operatora
operatorc
operatore
operatorg
operatori
operators
operat
opera
oper
ope
op
o

To this doesn't follow any sensible pattern or indicates some weird idea of "every second letter". Even if the length were the criteria what sense would it make? It would be better to get rid of the inspection altogether instead of having one that works semi-randomly.

Besides, why would shorter length result in bigger search cost? I know this would make sense if I searched for a symbol globally, then there would be more instances of short symbols than longer ones. But in this case PhpStorm should only look for the usages of properties belonging to this class and if this class is not used anywhere then there's nothing really to search for.

 

0
Comment actions Permalink

It would be better to get rid of the inspection altogether instead of having one that works semi-randomly.

It works this way only in the editor, in the batch inspection mode, the results will be proper.

Besides, why would shorter length result in bigger search cost? I know this would make sense if I searched for a symbol globally, then there would be more instances of short symbols than longer ones. But in this case PhpStorm should only look for the usages of properties belonging to this class and if this class is not used anywhere then there's nothing really to search for.

It doesn't invoke Find Usages on each symbol while in the editor mode, it's proven to cause performance issues. Instead, it just looks for a number of indexes for the name. If it's more than n (I am not sure how much exactly, but it doesn't matter), the inspection is not being triggered.

0

Please sign in to leave a comment.