PHP-CS-Fixer:格式化你的PHP代碼


簡介

良好的代碼規范可以提高代碼可讀性,團隊溝通維護成本。最推薦大家遵守的是 php-fig(PHP Framework Interop Group) 組織定義的 PSR-1PSR-2 兩個。不了解的同學可以先通過鏈接點擊過去閱讀下。

這個工具的作用就是按照 PSR-1PSR-2 的規范格式化你的代碼。

安裝

PHP需求:PHP最小版本5.3.6。

本地安裝
安裝很簡單,下載php-cs-fixer.phar文件就行了。官方地址是:
http://get.sensiolabs.org/php-cs-fixer.phar

國內的朋友如果下載很慢,可以使用百度雲:
鏈接: http://pan.baidu.com/s/1qWUTd5y 密碼: yith

Composer方式安裝
如果你還不了解Composer,請點擊鏈接查看。

新建composer.json

{
	"require" :{
		"fabpot/php-cs-fixer":"*"
	},"config": {
        "secure-http": false
    }
}

運行:

composer update

稍等片刻,下載完成:目錄生成了vendor文件夾。

設置全局:

export PATH="$PATH:$HOME/.composer/vendor/bin"

注意,composer安裝的與本地方式安裝后調用的執行文件是不一樣的。本地安裝執行的是php-cs-fixer.phar;composer安裝的執行的是php vendor\fabpot\php-cs-fixer\php-cs-fixer

homebrew安裝

$ brew install homebrew/php/php-cs-fixer

如何使用

命令行運行:

php-cs-fixer

會給出很多幫助提示。

使用 fix 指令修復文件夾或文件的代碼風格

php php-cs-fixer.phar fix /path/to/dir
php php-cs-fixer.phar fix /path/to/file

選項:

--format 輸出文件格式,支持txt、xml
--verbose 
--level 應用哪種PSR類型。支持psr0、psr1、psr2。默認是psr2
--dry-run 顯示需要修復但是沒有修復的代碼
php php-cs-fixer.phar fix /path/to/project --level=psr0
php php-cs-fixer.phar fix /path/to/project --level=psr1
php php-cs-fixer.phar fix /path/to/project --level=psr2
php php-cs-fixer.phar fix /path/to/project --level=symfony

示例:

$ php php-cs-fixer.phar fix test.php

   1) test.php
Fixed all files in 0.290 seconds, 4.250 MB memory used

有一些要注意的地方是,php-cs-fixer 因為是在不破壞相容性的前提下修正的,所以有些 方法命名 的規則就無法修。不過比起手動修正,可以省下不少時間。

更多使用方式參見 Usage

使用.php_cs文件

在一些開源框架中都看到了 .php_cs 文件。這個文件便是php-cs-fixer的格式化配置。

官方是這么描述的:

Instead of using command line options to customize the fixer, you can save the configuration in a .php_cs file in the root directory of your project. 

如何使用.php_cs

$ php php-cs-fixer fix --config-file .php_cs  test.php

Loaded config from ".php_cs"
   1) test.php
Fixed all files in 0.242 seconds, 4.250 MB memory used

使用--config-file加載.php_cs文件。文件內容詳情見文末。

升級

php php-cs-fixer.phar self-update

或者

$ sudo php-cs-fixer self-update

composer方式:

$ ./composer.phar global update fabpot/php-cs-fixer

brew方式:

$ brew upgrade php-cs-fixer

StyleCI介紹

當我們使用 PHP-CS-Fixer 讓我們現有的代碼規范化之后,我們怎么確保以后開發的代碼,以及別人 pr 的代碼都能正確的符合代碼風格規范呢?

StyleCI 是一個 Laravel5 項目,功能實現也是由 PHP-CS-Fixer 驅動。

它可以自己分析你項目的 pull request,並且在你 merge 前顯示出分析的結果。

該工具沒有具體使用過,下面是它的官網,感興趣的同學可以看看。

官方網站:https://styleci.io/

相關資源

PHP Coding Standards Fixer--FriendsOfPHP
https://github.com/FriendsOfPHP/PHP-CS-Fixer

使用 PHP-CS-Fixer 自動規范化你的 PHP 代碼_PHPHub - PHP & Laravel的中文社區
https://phphub.org/topics/547

現在寫 PHP,你應該知道這些 - Scholer 的 PHP 之路 - SegmentFault
https://segmentfault.com/a/1190000003844380

Basic php-cs-fixer rules
https://github.com/XiaoLer/php-develop-standards/blob/master/php-cs-fixer-rules.md

.php_cs內容參考

<?php

$header = <<<EOF
This file is part of the PHP CS utility.

(c) Fabien Potencier <fabien@symfony.com>

This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);

return Symfony\CS\Config\Config::create()
    // use default SYMFONY_LEVEL and extra fixers:
    ->fixers(array(
        'header_comment',
        'long_array_syntax',
        'ordered_use',
        'php_unit_construct',
        'php_unit_strict',
        'strict',
        'strict_param',
    ))
    ->finder(
        Symfony\CS\Finder\DefaultFinder::create()
            ->exclude('Symfony/CS/Tests/Fixtures')
            ->in(__DIR__)
    )
;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM