php 使用php-cs-fixer统一代码风格

 

.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,    // 遵循PSR2
        //'@Symfony'                              => false,
        //'@Symfony:risky'                        => false,
        '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 排序,不使用,按编辑器的默认排序
        //'ordered_imports'                   => [
        //    'sort_algorithm' => 'length',                  // 类的use按长度由小到大排序
        //],
        'phpdoc_add_missing_param_annotation'   => true,  //添加缺少的 Phpdoc @param参数
        'phpdoc_trim'                           => true,
        //'phpdoc_trim_consecutive_blank_line_separation' => true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。
        'phpdoc_order'                          => false,
        '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);

 

format-php-code.bat文件:

@echo off&title PHP格式化代码&color 0F
rem 清屏
cls
rem 设置编码
set LESSCHARSET=utf-8

set file1=.
set file2=E:\www\test\api
set file3=E:\dnmp\www\test\store
set file4=E:\dnmp\www\test\group

echo **************************************************************
echo *                     格式化代码帮助信息                          
echo *  Author:L                                                 
echo *  Created:2020/12/12                                       
echo **************************************************************
echo *  1: 当前目录  %file1%
echo *  2: 指定目录  %file2%
echo *  3: 指定目录  %file3%
echo *  4: 指定目录  %file4%
echo **************************************************************
echo;

set input=1
set /p "input=请输入[默认1]:"
if "%input%"=="1" (
    set files=%file1%
)
if "%input%"=="2" (
    set files=%file2%
)
if "%input%"=="3" (
   set files=%file3%
)
if "%input%"=="4" (
   set files=%file4%
)
call :formatCode %files%
pause

:formatCode
echo 格式化代码路径 %1
if "%1" == "" (
    echo 目录路径有误
) else (
    cls
    php php-cs-fixer.phar fix --config=.php_cs.dist %1
)

 

https://github.com/FriendsOfPHP/PHP-CS-Fixer

到上面地址下载php-cs-fixer.phar这个文件

 

最终文件结构:

 

使用示例:

 

posted @ 2020-12-10 18:21  一个人的孤独自白  阅读(762)  评论(0编辑  收藏  举报