简体   繁体   English

如何在PhpStorm中找到未使用的常量?

[英]How to find unused constants in PhpStorm?

I used to get highlights for any unused constants or class constants in PhpStorm. 我曾经在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 我曾尝试使用phpmd(PHP代码混乱检测器),并尝试过PHP CodeSniffer
  • I've tried PHP Inspections (EA Extended) 我已经尝试过PHP Inspections(EA Extended)

Declaration / Highlights of unused constants used to show automatically without running any inspection or commands. 声明/未使用常量的亮点,用于自动显示而不运行任何检查或命令。

Not the answer you wanted but this is not possible as of yet . 不是答案你想,但这是不可能的,因为 1 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: 目前实现这一目标的唯一方法是给你的常量一个private修饰符,如下图所示:

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). 并且这个方法并不完全理想,因为它正在拾取private而不是const (更不用说,你可能希望const有问题的const )。

The second option would be to create a custom inspection setting plugin and define it and again, this is not ideal. 第二个选项是创建自定义检查设置插件并再次定义,这不是理想的选择。

1 I 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. 1我已经创建了一个功能请求,并且通过此赏金的宣传可以实现此功能,您可以在此处找到功能请求,您也可以对其进行投票。

you can change all Inspections belong to PHP in settings > Editor > Inspections 您可以在settings > Editor > Inspections更改属于PHP的所有settings > Editor > Inspections

在此输入图像描述

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 : 现在,当在函数内定义一个不使用的变量或者Function有一个未使用的参数时, Phpstorm显示警告: 在此输入图像描述

在此输入图像描述

PhpStorm is able to instantly highlight classes, methods and fields which are unused across the entire project via Unused declarations inspection. PhpStorm能够通过未使用的声明检查立即突出显示整个项目中未使用的类,方法和字段。

For running an inspection, helpful link is: https://www.jetbrains.com/help/phpstorm/running-inspections.html 要运行检查,有用的链接是: https//www.jetbrains.com/help/phpstorm/running-inspections.html

I've been working with PhpStorm for long time but I never heard you can highlight unused constants or method. 我一直在使用PhpStorm很长一段时间,但我从未听说过你可以突出显示未使用的常量或方法。 Even developer of the phpstorm confirm this. 即使是phpstorm的开发者也证实了这一点。 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. 就像Script47所说,PhpStorm检查是否使用了特定的变量,常量,方法,因为你只能在同一个类中使用私有访问修饰符。 在此输入图像描述

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 Ctrl + Alt + 7或转到编辑 - >查找 - >显示用法

if it's not used in anywhere in your code you can know. 如果它没有在代码中的任何地方使用,你就可以知道。

在此输入图像描述

For javascript you can achieve this easily. 对于javascript,您可以轻松实现此目的。 By default PhpStorm highlight unused variable and methods: 默认情况下,PhpStorm突出显示未使用的变量和方法:

在此输入图像描述 在此输入图像描述

If you able to find a better solution please post an answer, I need this too :) 如果你能找到更好的解决方案,请发一个答案,我也需要这个:)

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. PhpStorm或任何其他IDE无法确定属性或常量的100%使用率,因为这些只能执行脚本检查而不是功能检查。

There are cases where usage can not be determined. 有些情况下无法确定使用情况。

  1. Accessing property/method dynamically like $obj->{$propertry} . 动态访问属性/方法,如$obj->{$propertry}
  2. Using magic method _get() or _set() for property. 对属性使用magic方法_get()_set()
  3. Calling methods with user defined functions call_user_func_array() . 使用用户定义的函数call_user_func_array()调用方法。
  4. Using constant() function to get constants. 使用constant()函数获取常量。

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. IDE的使用功能,它将为类提供硬编码使用常量,并从其父/子类继承使用。
  2. You can do complete text search in your project like ::<YOUR_CONSTANT> , it will give you complete usage of that constant. 您可以在项目中执行完整的文本搜索,例如::<YOUR_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() . 仅对于private变量,phpstorm可以突出显示变量是否未使用,如果不使用constant()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM