如何在 PhpStorm 使用 SonarLint ?
讓 SonarQube 即時在 PhpStorm 內檢查
Contents
SonarQube 除了搭配 Jenkins 檢查程式碼品質外,還可以在 IDE 中使用 SonarLint,讓 developer 在程式開發階段就及早發現可能的 bugs、vulenrability、code smell 與 duplication,本文將介紹 SonarLint + PhpStorm。
Version
macOS High Sierra 10.13.4
SonarQube 6.7.2 LTS
PhpStorm 2018.1
SonarLint 3.3.0.2482
PHP 7.1.14
Laravel 5.6.17
安裝 SonarLint

- PhpStorm -> Preferences … -> Plugins
- 按
Browse repositories…

- 輸入
SonarLint - 按
Install安裝

- 下載完 SonarLint 後,按
Restart PhpStorm
General Setting
Settings

- PhpStorm -> Preferences -> Other Settings -> SonarLint General Settings
- 選擇
Settingstab - 勾選
Automatically trigger analysis,SonarLint 將會自動檢查目前的檔案 - 在
SonarQube servers按+新增 server

- 為設定取一個名字
- 輸入 SonarQube 的網址
- 按
Next繼續

Authentication type選擇Login / Password- Login / Password 填預設的
admin/admin - 按
Next繼續

- 按
Finish完成設定

- 按
Update binding從 SonarQube server 更新 project list、profile、rule 到本機
File Exclusions

- 選擇
File Exclusionstab,若每個專案都有特定的目錄不想被 SonarLint 檢查,可統一設定在此
Project Setting
Bind to SonarQube project

- PhpStorm -> Preferences -> Other Settings -> SonarLint Project Settings
- 選擇
Bind to SonarQube projecttab - 將
Enable binding to remote SonarQube server打勾,如此本地專案將與 SonarQube server 端綁定 Bind to server選擇剛剛建立的MySonarQubeSonarQube project選擇欲綁定的SonarQube專案
File Exclusion

- 選擇
File Exclusionstab,若專案中有特定的目錄不想被 SonarLint 檢查,可設定在此 - 按
OK儲存設定
Automatic Analysis

SonarQube 檢查出在 app/Console/Kernel.php 有 code smell,不該在程式碼有註解掉的 code,而應該直接刪除,由 Git 作版控。

SonarLint 能在 PhpStorm 內即時的檢查出目前檔案的 issue。
- 下方選擇
SonarLinttab - 有問題的 code
- SonarLint 的錯誤訊息,與 SonarQube server 完全一樣,
- 解釋該 rule 檢查理由
Conclusion
- 有了 SonarLint,developer 就能更即時的獲得 SonarQube 的建議,養成寫出 clean code 的好習慣