[英]How do I use Psalm's UnusedMethod Feature?
我正在嘗試使用PHP的psalm靜態分析工具 。 我的理解是這個工具可以告訴我代碼庫中未使用的方法 。 但是,如果我創建一個簡單的測試文件
#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>
Psalm的創建者 - 死代碼檢測只檢測整個項目時未使用的類和方法 - 例如./vendor/bin/psalm --find-dead-code
,省略src/test.php
。
雖然私有方法和屬性是一種特殊情況(可以在不檢查整個項目的情況下推斷它們的非使用),但對於公共/受保護的方法和屬性,必須消耗所有內容。
根據該文件 ,你要使用--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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.