如何使用 Psalm 的 UnusedMethod 功能?

How do I use Psalm's UnusedMethod Feature?

我正在尝试使用 psalm static analysis tool for PHP. It's my understanding that this tool can tell me about unused methods in my codebase。但是,如果我创建一个简单的测试文件

#File: src/test.php
<?php
class A {
    private function foo() : void {}
}

new A();

然后 运行 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

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

没有发现错误。

为什么 psalm 找不到未使用的方法 foo?是否需要额外的配置?还是我误解了这个工具的作用?我的 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>

According to the documentation,您需要将 --find-dead-code 参数用于 psalm:

./vendor/bin/psalm --find-dead-code foo.php

输出:

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

Psalm creator here - 死代码检测仅在分析整个项目时检测未使用的 类 和方法 - 例如./vendor/bin/psalm --find-dead-code,省略 src/test.php

虽然私有方法和属性是一种特殊情况(无需检查整个项目就可以推断出它们未被使用),但对于 public/protected 方法和属性,所有内容都必须被使用。