良好的代码规范能够提升代码可读性,团队沟通维护成本。最推荐你们遵照的是 php-fig(PHP Framework Interop Group) 组织定义的 PSR-1 、 PSR-2 两个。不了解的同窗能够先经过连接点击过去阅读下。php
这个工具的做用就是按照 PSR-1
和 PSR-2
的规范格式化你的代码。html
PHP需求:PHP最小版本5.3.6。git
本地安装
安装很简单,下载php-cs-fixer.phar文件就好了。官方地址是:
http://get.sensiolabs.org/php-cs-fixer.phargithub
国内的朋友若是下载很慢,可使用百度云:
连接: http://pan.baidu.com/s/1qWUTd5y 密码: yithjson
Composer方式安装
若是你还不了解Composer,请点击连接查看。segmentfault
新建composer.jsoncomposer
{ "require" :{ "fabpot/php-cs-fixer":"*" },"config": { "secure-http": false } }
运行:框架
composer update
稍等片刻,下载完成:目录生成了vendor文件夹。ide
设置全局:工具
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、psr一、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-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
当咱们使用 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 $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__) ) ;