我正在尝试将psalm 静态分析工具用于 PHP。我的理解是该工具可以告诉我代码库中未使用的方法。但是,如果我创建一个简单的测试文件
#File: src/test.php
<?php
class A {
private function foo() : void {}
}
new A();
Run Code Online (Sandbox Code Playgroud)
然后运行 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
Run Code Online (Sandbox Code Playgroud)
或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 …Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以指定函数将返回特定类型的对象,其中类型是参数之一的字符串?
例如
/**
* @return object<$class>
*/
public function create(string $class): object {
... some factory stuff
}
Run Code Online (Sandbox Code Playgroud)
这样 vscode 或 phpstorm 就会知道当我这样做时
$myvar = X::create('MyClass');
Run Code Online (Sandbox Code Playgroud)
$myvar 将是 MyClass 类型,我将拥有适当的智能感知/自动完成功能?