[英]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: 我尝试过的:
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: 脚步:
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. 有些情况下无法确定使用情况。
$obj->{$propertry}
. $obj->{$propertry}
。 _get()
or _set()
for property. _get()
或_set()
。 call_user_func_array()
. call_user_func_array()
调用方法。 constant()
function to get constants. constant()
函数获取常量。 There are multiple steps you can do to find usages for constant. 您可以通过多个步骤找到常量的用法。
::<YOUR_CONSTANT>
, it will give you complete usage of that constant. ::<YOUR_CONSTANT>
,它将为您提供该常量的完整用法。 Only for
private
variables, phpstorm can highlight if variable is unused, that too if you don't useconstant()
.仅对于
private
变量,phpstorm可以突出显示变量是否未使用,如果不使用constant()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.