Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unused constants in PhpStorm?

I used to get highlights for any unused constants or class constants in PhpStorm.

Now I can't get it work again. Maybe it was a plugin, I'm not sure.

What I've tried:

  • I've tried to run the code inspector
  • I've tried to use phpmd (PHP code mess detector) and also tried PHP CodeSniffer
  • I've tried PHP Inspections (EA Extended)

Declaration / Highlights of unused constants used to show automatically without running any inspection or commands.

like image 923
Devy Avatar asked Jun 15 '18 22:06

Devy


People also ask

Where is unused code PHPStorm?

It's possible in PHPStorm 7/8. Go to Code > Inspect Code and use a custom inspection profile. In the Inspections window, click on Add and unselect everything except PHP > Unused. That's only the unused private method I mention.

What is an unused declaration?

The description of "Unused declaration is: "This inspection reports classes, methods or fields in the specified inspection scope that are not used or not reachable from entry points." The classes, field and methods that IDEA are complaining about are all used/started etc.

How do I find unused variables in IntelliJ?

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group). @CrazyCoder This shows unused fields and methods too, any way to show only unused classes? My 2 cents: on IntelliJ 2016.3. 4 the option is under Analyze > Run Inspection By Name.


1 Answers

Not the answer you wanted but this is not possible as of yet.1

I scoured through the plugin repository and the inspection options and couldn't find anything for this specific request.

The only way that this can be achieved currently is by giving your constant a private modifier as can be seen in the image below:

and this method is not exactly ideal as it is picking up the private rather than the const (not to mention, you might want to globalise the const in question).

The second option would be to create a custom inspection setting plugin and define it and again, this is not ideal.

1I have created a feature request and with the publicity of this bounty it may be possible to get this implemented, you can find the feature request here and you can vote on it too.

like image 176
Script47 Avatar answered Oct 12 '22 10:10

Script47