PHPStorm 保存时自动执行 PHP CS Fixer

在我们日常开发中,一定少不了 PHP CS Fixer 来帮我们统一代码风格,但是PHP CS Fixer 不像 ESLint 一样,可以在 PHPStorm 中在保存时自动执行。

PHPStorm 保存时自动执行 PHP CS Fixer

PHPStorm 并没有为我们提供可执行 PHP CS Fixer 的选项,「重新格式化代码」大部分时都不能满足我们的需求。


为此我们需要在 PHPStorm 中添加一个 「File Watcher」来自动执行代码格式化。

  1. 首先全局安装 PHP CS Fixer
    composer global require friendsofphp/php-cs-fixer
  2. 执行
    php-cs-fixer

PHPStorm 保存时自动执行 PHP CS Fixer

代表安装成功了,如果提示命令未找到,那么你需要将全局 composer vendor 目录添加到全局变量,我用的是 zsh,这里改成你自己的。

echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.zshrc
  1. 打开 PHPStorm,添加自定义文件
    PHPStorm 保存时自动执行 PHP CS Fixer
    程序文件地址,命令行输入,并填入
    which php-cs-fixer
    PHPStorm 保存时自动执行 PHP CS Fixer
    参数栏:
    fix $FileDir$/$FileName$

到这就搞定了,现在每当我们保存时就会自动执行 php-cs-fixer,现在还有一个问题,是可能每个项目有不同的 .php-cs.dist 格式化配置文件,以上的配置是使用了全局 php-cs-fixer 配置文件,如果要使用单独的配置文件,需要修改配置如下:

fix --config=$ProjectFileDir$/.php-cs.dist $FileDir$/$FileName$

.php-cs.dist 通常放在项目根目录。

最后附上 .php-cs.dist 配置文件

<?php
$header = <<<'EOF'

EOF;

$finder =  PhpCsFixer\Finder::create()
            ->exclude('tests/Fixtures')   //排除文件
            ->in(__DIR__);

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR2'                                 => true,
        '@Symfony:risky'                        => true,
        'array_syntax'                          => ['syntax' => 'short'],
        'combine_consecutive_unsets'            => true,   //多个unset,合并成一个
        // one should use PHPUnit methods to set up expected exception instead of annotations
        'general_phpdoc_annotation_remove'      => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'], //phpdocs中应该省略已经配置的注释
        //'header_comment'                        => array('header' => $header), //添加,替换或者删除 header 注释。
        'heredoc_to_nowdoc'                     => true,     //删除配置中多余的空行和/或者空行。
        'no_extra_consecutive_blank_lines'      => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
        'no_unreachable_default_argument_value' => false, //在函数参数中,不能有默认值在非缺省值之前的参数。有风险
        'no_useless_else'                       => true,  //删除无用的eles
        'no_useless_return'                     => true,  //删除函数末尾无用的return
        'no_empty_phpdoc'                       => true,  // 删除空注释
        'no_empty_statement'                    => true,  //删除多余的分号
        'no_leading_namespace_whitespace'       => true,  //删除namespace声明行包含前导空格
        'no_spaces_inside_parenthesis'          => true,  //删除括号后内两端的空格
        'no_trailing_whitespace'                => true,  //删除非空白行末尾的空白
        'no_unused_imports'                     => true,  //删除未使用的use语句
        'no_whitespace_before_comma_in_array'   => true,  //删除数组声明中,每个逗号前的空格
        'no_whitespace_in_blank_line'           => true,  //删除空白行末尾的空白
        'ordered_class_elements'                => false, //class elements排序
        'ordered_imports'                       => false, // use 排序
        'phpdoc_add_missing_param_annotation'   => true,   //添加缺少的 Phpdoc @param参数
        'phpdoc_trim'                           => true,
        //   'phpdoc_trim_consecutive_blank_line_separation' => true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。
        'phpdoc_order'                          => true,
        'psr4'                                  => true,
        // 'strict_comparison'                     => true,   //严格比较,会修改代码有风险
        //'strict_param'                          => true,
        'ternary_operator_spaces'               => true,  //标准化三元运算的格式
        'ternary_to_null_coalescing'            => true,  //尽可能使用null合并运算符??。需要PHP> = 7.0。
        'whitespace_after_comma_in_array'       => true, // 在数组声明中,每个逗号后必须有一个空格
        'trim_array_spaces'                     => true,  //删除数组首或尾随单行空格
        'align_multiline_comment'               => [                   //每行多行 DocComments 必须有一个星号(PSR-5),并且必须与第一行对齐。
            'comment_type' => 'phpdocs_only'
        ],
        'array_indentation'                 => true,  //数组的每个元素必须缩进一次
    ])
    ->setFinder($finder);
本作品采用《CC 协议》,转载必须注明作者和本文链接
悲观者永远正确,乐观者永远前行。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2
陈先生

贫穷的我买不起一台可以运行 PHPStorm 的 MacBookPro

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!