การวิเคราะห์รหัสแบบคงที่สำหรับความคิด phpstorm และ intellij
Open 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 tag
/**
* @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
} ประเภทที่ระบุไว้ใน PHP สามารถละเว้นได้ในบล็อก phpDoc
/**
* @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 ใช้ Assert เพื่อตรวจสอบประเภทตัวแปรแทนความคิดเห็นของ 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 ){}
} เรียกใช้ refactor this ในชื่อเมธอด fromName และข้อความใหม่ทั้งหมดที่มีคลาสนี้จะมีการเปลี่ยนแปลง
new Text ( ' User ' ) // old code
Text:: fromName ( ' User ' ) // new code