PhpClean
2023.12.17
PhpStorm和Intellij Idea的静态代码分析。
打开IDE转到Settings->Plugins->Marketplace搜索PhpClean 。点击install按钮。
- 检查
- 动作
在一个声明中检测分配和比较操作员。
while ( false !== $ current = ldap_next_entry ( $ con , $ current )) {
// ^^^ Hard to read this statements
yield $ this -> getSingleEntry ( $ con , $ current );
}在不同名称空间中具有相同名称的类可能会混淆。 (默认禁用)
namespace App {
class User {}; // <- Class name collision with CliUser
}
namespace Cli {
class User {}; // <- Class name collision with AppUser
}您可以在项目中删除一些PHPDOC标签。
该检查检测全球变量的用法。
echo $ _GET [ ' name ' ]; // <-- Global variable usage 受保护的方法可以转换为私人。
final class User {
protected function name () {} // <-- Method can be private
}方法应关闭(制作方法或班级最终)
class User {
public function name (): string { // <-- Method should be final
return '' ;
}
}受保护的方法使我们的课程更加开放。仅写私人或公共方法。
始终指定参数类型。这是一个很好的做法。
class User {
public function withName ( $ name ) {} // <-- Missing parameter type
}始终指定功能的结果类型。
function phrase () { // <-- Missing return type
return ' hi ' ;
}检查父属性是否已弃用。
class A {
/** @deprecated */
protected $ name ;
}
class B extends A {
protected $ name ; // <-- Warn about deprecation
}标记为@final Doc标签的类不应扩展
/**
* @final
*/
class User {};
class Admin extends User {}; // <- Prohibited extentions of @final class User. 在构造函数中未初始化的属性应注释为无效。
class User {
/** @var string */ // <-- Property is not annotated correctly. Add null type
private $ name ;
public function getName () { }
public function setName ( string $ name ) { }
}受保护的属性可以转换为私人。
class User {
protected $ user ; // <-- Property can be private
}可以在PHPDOC块中省略PHP中指定的类型
/**
* @return void // <-- Redundant PhpDoc tag
*/
function show ( string $ message ): void {}检测自动式铸造
class Hello {
public function randomize (): self { /* ... */ return $ this ; }
public function __toString () { return ' Hi ' ; }
}
echo ( new Hello ())-> randomize (); // <-- Deprecated __toString call 使用断言检查变量类型而不是DOC注释。
/** @var User $user */ // <-- Use assert to check variable type
assert ( $ user instanceof User);将new ClassName()替换为选定的命名构造函数。
class Text {
public function __construct ( string $ name ){ }
public static fromName (string $ n ){}
}在方法fromName中调用refactor this方法名称和此类的所有新语句
new Text ( ' User ' ) // old code
Text:: fromName ( ' User ' ) // new code