6

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.

Script47
  • 14,230
  • 4
  • 45
  • 66
Devy
  • 248
  • 10
  • 20
  • Possible duplicate of [How to use IntelliJ IDEA to find all unused code?](https://stackoverflow.com/questions/6587729/how-to-use-intellij-idea-to-find-all-unused-code) – ollaw Jun 15 '18 at 22:32
  • @Ollaw I tried it but not showing unused constants, also i used to see it in the file itself without running anything – Devy Jun 15 '18 at 22:36
  • Did you use 3rd-party plugin like PHP Inspections (EA Extended)? Might be helpful. – Vlad Luchansky Jun 16 '18 at 00:46
  • PhpStorm does not notify about unused constants ... unless it's a class constant with `private` visibility. As Vlad have said -- it may come from another plugin. – LazyOne Jun 17 '18 at 08:42

5 Answers5

1

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.

Script47
  • 14,230
  • 4
  • 45
  • 66
0

you can change all Inspections belong to PHP in settings > Editor > Inspections

enter image description here

And now, when within a function, define a variable that does not use or Function has a parameter that is not used, Phpstorm shows warning : enter image description here

enter image description here

Alihossein shahabi
  • 4,034
  • 2
  • 33
  • 53
0

PhpStorm is able to instantly highlight classes, methods and fields which are unused across the entire project via Unused declarations inspection.

For running an inspection, helpful link is: https://www.jetbrains.com/help/phpstorm/running-inspections.html

Stuti Verma
  • 1,059
  • 13
  • 32
0

I've been working with PhpStorm for long time but I never heard you can highlight unused constants or method. Even developer of the phpstorm confirm this. But there are two ways you could do similar task.

Private access modifier

Like Script47 said PhpStorm check whether that particular variable,constants, method used since you can only use private access modifier in same class. enter image description here

Do it by individually

You can check whether that particular variable,constants, method used by each individually.

steps:

  1. Click on the variable, constants, method you want to check.
  2. press Ctrl+Alt+7 or go to edit->find->show Usage

if it's not used in anywhere in your code you can know.

enter image description here

For javascript you can achieve this easily. By default PhpStorm highlight unused variable and methods:

enter image description here enter image description here

If you able to find a better solution please post an answer, I need this too :)

Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33
0

PhpStorm or any other IDE can not determine 100% usage of a property or constant, since these can only do script check not functional check.

There are cases where usage can not be determined.

  1. Accessing property/method dynamically like $obj->{$propertry}.
  2. Using magic method _get() or _set() for property.
  3. Calling methods with user defined functions call_user_func_array().
  4. Using constant() function to get constants.

There are multiple steps you can do to find usages for constant.

  1. The usage feature of IDE, it will give hardcoded usage of constant for the class and inherited usage from its parent/child classes.
  2. You can do complete text search in your project like ::<YOUR_CONSTANT>, it will give you complete usage of that constant.
  3. If you have better code coverage tests, you can remove constant from class and it's parent class to see for failures.

Only for private variables, phpstorm can highlight if variable is unused, that too if you don't use constant().

anwerj
  • 2,386
  • 2
  • 17
  • 36