登录  /  注册

图文详解PHPStorm实现自动执行代码格式化

藏色散人
发布: 2022-12-29 17:36:43
转载
2727人浏览过

在我们日常开发中,一定少不了 php cs fixer 来帮我们统一代码风格,但是php cs fixer 不像 eslint 一样,可以在 phpstorm 中在保存时自动执行。

414667894785b35a6f5df1e362ece30.jpg

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


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

1.首先全局安装 PHP CS Fixer

composer global require friendsofphp/php-cs-fixer
登录后复制

2.执行

php-cs-fixer
登录后复制

bbfc8a7917d4c83e1da84156f464ed8.jpg

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

echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.zshrc
登录后复制

3.打开 PHPStorm,添加自定义文件

748c716bd476c49678551f9910c2cf3.jpg

程序文件地址,命令行输入,并填入

which php-cs-fixer
登录后复制

5666432f8ec6918a55caf7d337aa14b.jpg

参数栏:

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 = <<<&#39;EOF&#39;EOF;$finder =  PhpCsFixer\Finder::create()
            ->exclude('tests/Fixtures')   //排除文件
            -&gt;in(__DIR__);return PhpCsFixer\Config::create()
    -&gt;setRiskyAllowed(true)
    -&gt;setRules([
        '@PSR2'                                 =&gt; true,
        '@Symfony:risky'                        =&gt; true,
        'array_syntax'                          =&gt; ['syntax' =&gt; 'short'],
        'combine_consecutive_unsets'            =&gt; true,   //多个unset,合并成一个
        // one should use PHPUnit methods to set up expected exception instead of annotations
        'general_phpdoc_annotation_remove'      =&gt; ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'], //phpdocs中应该省略已经配置的注释
        //'header_comment'                        =&gt; array('header' =&gt; $header), //添加,替换或者删除 header 注释。
        'heredoc_to_nowdoc'                     =&gt; true,     //删除配置中多余的空行和/或者空行。
        'no_extra_consecutive_blank_lines'      =&gt; ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
        'no_unreachable_default_argument_value' =&gt; false, //在函数参数中,不能有默认值在非缺省值之前的参数。有风险
        'no_useless_else'                       =&gt; true,  //删除无用的eles
        'no_useless_return'                     =&gt; true,  //删除函数末尾无用的return
        'no_empty_phpdoc'                       =&gt; true,  // 删除空注释
        'no_empty_statement'                    =&gt; true,  //删除多余的分号
        'no_leading_namespace_whitespace'       =&gt; true,  //删除namespace声明行包含前导空格
        'no_spaces_inside_parenthesis'          =&gt; true,  //删除括号后内两端的空格
        'no_trailing_whitespace'                =&gt; true,  //删除非空白行末尾的空白
        'no_unused_imports'                     =&gt; true,  //删除未使用的use语句
        'no_whitespace_before_comma_in_array'   =&gt; true,  //删除数组声明中,每个逗号前的空格
        'no_whitespace_in_blank_line'           =&gt; true,  //删除空白行末尾的空白
        'ordered_class_elements'                =&gt; false, //class elements排序
        'ordered_imports'                       =&gt; false, // use 排序
        'phpdoc_add_missing_param_annotation'   =&gt; true,   //添加缺少的 Phpdoc @param参数
        'phpdoc_trim'                           =&gt; true,
        //   'phpdoc_trim_consecutive_blank_line_separation' =&gt; true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。
        'phpdoc_order'                          =&gt; true,
        'psr4'                                  =&gt; true,
        // 'strict_comparison'                     =&gt; true,   //严格比较,会修改代码有风险
        //'strict_param'                          =&gt; true,
        'ternary_operator_spaces'               =&gt; true,  //标准化三元运算的格式
        'ternary_to_null_coalescing'            =&gt; true,  //尽可能使用null合并运算符??。需要PHP&gt; = 7.0。
        'whitespace_after_comma_in_array'       =&gt; true, // 在数组声明中,每个逗号后必须有一个空格
        'trim_array_spaces'                     =&gt; true,  //删除数组首或尾随单行空格
        'align_multiline_comment'               =&gt; [                   //每行多行 DocComments 必须有一个星号(PSR-5),并且必须与第一行对齐。
            'comment_type' =&gt; 'phpdocs_only'
        ],
        'array_indentation'                 =&gt; true,  //数组的每个元素必须缩进一次
    ])
    -&gt;setFinder($finder);
登录后复制

推荐学习:《PHPstorm使用教程》          

以上就是图文详解PHPStorm实现自动执行代码格式化的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号