優雅導入 PHP CS_Sniffer Linter
kylinyu.win
2021. 08. 10
Outline
composer install
讓 codesniffer 工具跟著各專案使用 composer 作版本控制
composer require squizlabs/php_codesniffer --dev
phpcs 檢查
(指定設定檔,優先抓設定檔內指定的位置)
(也可自行指定檢查 folder 位置或者 file
(不顯示 Warning 項目)
phpcbf 修正
(根據設定檔作修正)
設置 CS_Sniffer Configuration File
CS Sniffer 術語
Sniff 條規
意指: Standard 內的細目規則
PSR2 (12 sniffs)
….
Standard 標準
https://github.com/squizlabs/PHP_CodeSniffer/tree/master/src/Standards
掃描檔案設定
Standard 標準設定:
其餘設定:
如何微調規則 (Sniff)?
遇到某些錯誤如果是不適合修正的,e.g. namespace or class naming ...
phpcs --standard=phpcs.xml -e
> 找出 Sniff 名稱,再將對應條規新增到 phpcs.xml 作 exclude
單行 Ignore
PS. 還是建議修正不要使用 Ignore
// phpcs:disable Sniff Item
專案整合至 Legacy 專案
避免大量異動所產生的副作用及風險
採漸進式導入的方式做整合
coverageChecker
Makefile (半自動?
文章好讀版
Thank You
Any questions?