[英]How do I use Psalm's UnusedMethod Feature?
I'm trying to use the psalm static analysis tool for PHP . 我正在尝试使用PHP的psalm静态分析工具 。 It's my understanding that this tool can tell me about unused methods in my codebase . 我的理解是这个工具可以告诉我代码库中未使用的方法 。 However, if I create a simple test file 但是,如果我创建一个简单的测试文件
#File: src/test.php
<?php
class A {
private function foo() : void {}
}
new A();
and then run psalm
然后运行psalm
$ ./vendor/bin/psalm --find-dead-code src/test.php
Scanning files...
Analyzing files...
------------------------------
No errors found!
------------------------------
Checks took 0.16 seconds and used 32.694MB of memory
Psalm was able to infer types for 100% of the codebase
or psalter
, 或者是psalter
,
$ ./vendor/bin/psalter --find-unused-code --dry-run --issues=UnusedMethod src/test.php
Scanning files...
Analyzing files...
------------------------------
No errors found!
------------------------------
Checks took 0.05 seconds and used 29.214MB of memory
Psalm was able to infer types for 100% of the codebase
no errors are found. 没有发现错误。
Why isn't psalm finding the unused method foo
? 为什么psalm找不到未使用的方法foo
? Is there extra configuration that's needed? 是否需要额外的配置? Or do I misunderstand what this tool does? 或者我误解了这个工具的作用? My psalm.xml
file is below. 我的psalm.xml
文件如下。
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->
<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />
<InternalMethod errorLevel="info" />
<InternalProperty errorLevel="info" />
<InternalClass errorLevel="info" />
<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />
<MisplacedRequiredParam errorLevel="info" />
<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />
<RedundantCondition errorLevel="info" />
<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />
<UnresolvableInclude errorLevel="info" />
<RawObjectIteration errorLevel="info" />
<InvalidStringClass errorLevel="info" />
<UnusedMethod errorLevel="info" />
</issueHandlers>
</psalm>
Psalm creator here - dead code detection only detects unused classes and methods when the entire project is analysed - eg ./vendor/bin/psalm --find-dead-code
, omitting src/test.php
. Psalm的创建者 - 死代码检测只检测整个项目时未使用的类和方法 - 例如./vendor/bin/psalm --find-dead-code
,省略src/test.php
。
While private methods and properties are a special case (their non-use can be inferred without checking the entire project), for public/protected methods and properties everything must be consumed. 虽然私有方法和属性是一种特殊情况(可以在不检查整个项目的情况下推断它们的非使用),但对于公共/受保护的方法和属性,必须消耗所有内容。
According to the documentation , you'll want to use the --find-dead-code
argument to psalm
: 根据该文件 ,你要使用--find-dead-code
参数psalm
:
./vendor/bin/psalm --find-dead-code foo.php
Output: 输出:
Scanning files...
Analyzing files...
ERROR: UnusedVariable - foo.php:6:1 - Variable $a is never referenced
$a = new A();
------------------------------
1 errors found
------------------------------
Checks took 0.27 seconds and used 67.096MB of memory
Psalm was able to infer types for 100% of the codebase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.