PSR规范 php-cs-fixer 使用

前言

在Laravel课程上学习到使用 StyleCI 来规范PHP代码,但是公司的代码是搭建在Gogs上,没办法使用 StyleCI
最后想到 StyleCI 也是基于 PHP-CS-Fixer 构建的,因此直接安装相应扩展进行规范。

使用

安装

composer require --dev friendsofphp/php-cs-fixer

在项目根目录下新建.php_cs.dist文件,并编写需要执行的规则

PSR规范 php-cs-fixer 使用

<?php
$header = <<<EOF
This file is part of the mingzaily/lumen-permission.

(c) mingzaily <mingzaily@163.com>

This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

$finder = PhpCsFixer\Finder::create()
    ->files()
    ->name('*.php')
    ->exclude('vendor') //排除
    ->exclude('tests') // 排除
    ->in(__DIR__)
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

$rules = array(
    '@Symfony'                                   => true, // 开启预设的规则
    'header_comment'                             => array('header' => $header),
    'array_syntax'                               => array('syntax' => 'short'),
    'ordered_imports'                            => true, // 按顺序use导入
    'no_useless_else'                            => true, // 删除没有使用的else节点
    'no_useless_return'                          => true, // 删除没有使用的return语句
    'self_accessor'                              => true, //在当前类中使用 self 代替类名
    'php_unit_construct'                         => true,
    'single_quote'                               => true, //简单字符串应该使用单引号代替双引号
    'no_unused_imports'                          => true, //删除没用到的use
    'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法
    'no_empty_statement'                         => true, //多余的分号
    'no_whitespace_in_blank_line'                => true, //删除空行中的空格
    'standardize_not_equals'                     => true, //使用 <> 代替 !=
    'combine_consecutive_unsets'                 => true, // 当多个 unset 使用的时候,合并处理
    'concat_space'                               => ['spacing' => 'one'], // .拼接必须有空格分割
    'array_indentation'                          => true, // 数组的每个元素必须缩进一次
    'no_superfluous_phpdoc_tags'                 => false, // 移出没有用的注释
    'blank_line_before_statement'                => [
        'statements' => [
            'break',
            'continue',
            'declare',
            'return',
            'throw',
            'try'
        ]
    ],// 空行换行必须在任何已配置的语句之前
    'binary_operator_spaces'                     => [
        'default' => 'align_single_space'
    ], //等号对齐、数字箭头符号对齐
    'align_multiline_comment'                    => [
        'comment_type' => 'phpdocs_only'
    ],
    'lowercase_cast'                             => false,// 类型强制小写
    'lowercase_constants'                        => true,// 常量为小写
    'lowercase_static_reference'                 => true,// 静态调用为小写
    'no_blank_lines_after_class_opening'         => true,
    'phpdoc_separation'                          => false,// 不同注释部分按照单空行隔开
    'phpdoc_single_line_var_spacing'             => true,
    'phpdoc_indent'                              => true,
    'phpdoc_align'=>[
        'align'=>'vertical',
        'tags'=>[
            'param', 'throws', 'type', 'var', 'property'
        ]
    ]
);

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules($rules)
    ->setFinder($finder);

规则可以参考相应的 文档

执行

 vendor/bin/php-cs-fixer fix

就可以看见有哪些文件不符合规范被更改

PSR规范 php-cs-fixer 使用

写在后面

参考文章

最后打算有时间的话把PHP-CS-Fixer的规则翻译做个Wiki。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 5

666, @Symfony,这个要求蛮高的 :smile: :relieved:

3年前 评论
3年前 评论
mingzaily (楼主) 3年前

所有的$a++变成++$a了,怎么改回来

3年前 评论
mingzaily (楼主) 3年前

@柴星星

file

尝试在$rules中添加一个协议

$rules = [
    ...
    'increment_style' => false
    // 或者  'increment_style' => ['style'=>'post']
    ...
]
3年前 评论

再在 PhpStorm 上面配置 phpcs、php-cs-fixer 和 hook,nick work。

3年前 评论
mingzaily (楼主) 3年前
小李世界 (作者) 3年前
mingzaily (楼主) 3年前

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