• 技术文章 >后端开发 >php教程

    php-cs-fixer大有用处!能自动修证代码风格~

    藏色散人藏色散人2021-11-29 14:50:26转载1873

    开始

    最近在看 PHP 之道,看到 代码风格指南 章节的 php-cs-fixer。

    php-cs-fixer 是能够自动帮你修证代码风格,不仅仅是格式化。

    如果只需要代码保存时自动格式化的,PhpStorm 可以开启这个:

    233

    之前看别人发的项目,很多都是没有格式化的,至少以上 PhpStorm 保存时自动格式化也没有开启吧。

    接下来开始讲下,开启保存自动 php-cs-fixer 修正代码的方法。

    环境

    安装 php-cs-fixer

    这边使用全局安装

    composer global require friendsofphp/php-cs-fixer

    参见 https://cs.symfony.com/doc/installation.html

    在项目根路径下,新建文件:.php-cs-fixer.php,内容如下:

    <?phpuse PhpCsFixer\Config;use PhpCsFixer\Finder;$rules = [
        '@PHP80Migration' => true,
    
        'ordered_imports' => [
            'sort_algorithm' => 'alpha',
        ],
        'class_attributes_separation' => [
            'elements' => [
                'const' => 'one',
                'method' => 'one',
                'property' => 'one',
            ],
        ],];$finder = Finder::create()
        ->in([
            __DIR__.'/app',
            __DIR__.'/config',
            __DIR__.'/database',
            __DIR__.'/resources',
            __DIR__.'/routes',
            __DIR__.'/tests',
        ])
        ->name('*.php')
        ->notName('*.blade.php')
        ->ignoreDotFiles(true)
        ->ignoreVCS(true);return (new Config())
        ->setFinder($finder)
        ->setRules($rules)
        ->setRiskyAllowed(true)
        ->setUsingCache(true);

    然后在 PhpStorm 设置

    233

    233

    讲下可能需要讲的

    效果举例

    233

    当我们进行保存时,它就会自动修正代码,在这里是修正为 PHP 7 以上的风格。

    233

    在控制台就显示以下:

    开启控制台显示后

    /Users/dogeow/.composer/vendor/bin/php-cs-fixer fix /Users/dogeow/PhpstormProjects/antic-api/routes/console.php -vvv --diff
    Cannot load Xdebug - it was already loaded
    PHP CS Fixer 3.3.2 Trinacria by Fabien Potencier and Dariusz Ruminski
    Runtime: PHP 8.0.8
    Loaded config default from "/Users/dogeow/PhpstormProjects/antic-api/.php-cs-fixer.php".
    Using cache file ".php-cs-fixer.cache".
    Paths from configuration file have been overridden by paths provided as command arguments.
    F                                                                   1 / 1 (100%)
    Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
       1) routes/console.php (assign_null_coalescing_to_coalesce_equal)
          ---------- begin diff ----------
    --- /Users/dogeow/PhpstormProjects/antic-api/routes/console.php
    +++ /Users/dogeow/PhpstormProjects/antic-api/routes/console.php
    @@ -90,5 +90,5 @@
     });
    
     Artisan::command('test', function () {
    -    $taskTag['name'] = $taskTag['name'] ?? 'url';
    +    $taskTag['name'] ??= 'url';
     });
    
          ----------- end diff -----------
    
    
    Fixed all files in 0.024 seconds, 14.000 MB memory used
    
    进程已结束,退出代码为 0

    当然也可以手动在命令行执行来批量修正整个app 目录。或者使用 git 的,提交前自动修正等。

    推荐学习:《PHP视频教程

    以上就是php-cs-fixer大有用处!能自动修证代码风格~的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:learnku,如有侵犯,请联系admin@php.cn删除
    专题推荐:laravel php-cs-fixer
    上一篇:分享一个php处理信号的小例子 下一篇:php怎么获取指定日期是一周的第几天
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• php怎么判断指定日期是当年的第几天• php中关联数组和索引数组有什么区别• php怎么打乱数组随机选取几个数组元素• 来到1994年,终于知道为什么80%的网站都在用PHP了!
    1/1

    PHP中文网