[关闭]
@52fhy 2016-03-06T23:16:40.000000Z 字数 2980 阅读 616

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

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

  1. {
  2. "require" :{
  3. "fabpot/php-cs-fixer":"*"
  4. },"config": {
  5. "secure-http": false
  6. }
  7. }

运行:

  1. composer update

稍等片刻,下载完成:目录生成了vendor文件夹。

设置全局:

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

注意,composer安装的与本地方式安装后调用的执行文件是不一样的。本地安装执行的是php-cs-fixer.phar;composer安装的执行的是php vendor\fabpot\php-cs-fixer\php-cs-fixer

homebrew安装

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

如何使用

命令行运行:

  1. php-cs-fixer

会给出很多帮助提示。

使用 fix 指令修复文件夹或文件的代码风格

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

选项:

  1. --format 输出文件格式,支持txtxml
  2. --verbose
  3. --level 应用哪种PSR类型。支持psr0psr1psr2。默认是psr2
  4. --dry-run 显示需要修复但是没有修复的代码
  1. php php-cs-fixer.phar fix /path/to/project --level=psr0
  2. php php-cs-fixer.phar fix /path/to/project --level=psr1
  3. php php-cs-fixer.phar fix /path/to/project --level=psr2
  4. php php-cs-fixer.phar fix /path/to/project --level=symfony

示例:

  1. $ php php-cs-fixer.phar fix test.php
  2. 1) test.php
  3. Fixed all files in 0.290 seconds, 4.250 MB memory used

有一些要注意的地方是,php-cs-fixer 因为是在不破坏相容性的前提下修正的,所以有些 方法命名 的规则就无法修。不过比起手动修正,可以省下不少时间。

更多使用方式参见 Usage

使用.php_cs文件

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

官方是这么描述的:

  1. 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

  1. $ php php-cs-fixer fix --config-file .php_cs test.php
  2. Loaded config from ".php_cs"
  3. 1) test.php
  4. Fixed all files in 0.242 seconds, 4.250 MB memory used

使用--config-file加载.php_cs文件。文件内容详情见文末。

升级

  1. php php-cs-fixer.phar self-update

或者

  1. $ sudo php-cs-fixer self-update

composer方式:

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

brew方式:

  1. $ 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内容参考

  1. <?php
  2. $header = <<<EOF
  3. This file is part of the PHP CS utility.
  4. (c) Fabien Potencier <fabien@symfony.com>
  5. This source file is subject to the MIT license that is bundled
  6. with this source code in the file LICENSE.
  7. EOF;
  8. Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);
  9. return Symfony\CS\Config\Config::create()
  10. // use default SYMFONY_LEVEL and extra fixers:
  11. ->fixers(array(
  12. 'header_comment',
  13. 'long_array_syntax',
  14. 'ordered_use',
  15. 'php_unit_construct',
  16. 'php_unit_strict',
  17. 'strict',
  18. 'strict_param',
  19. ))
  20. ->finder(
  21. Symfony\CS\Finder\DefaultFinder::create()
  22. ->exclude('Symfony/CS/Tests/Fixtures')
  23. ->in(__DIR__)
  24. )
  25. ;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注