多人协同开发,保持统一代码风格

本文介绍如何使用Composer安装php-cs-fixer并配置.php-cs-fixer.php文件来自动修复PHP代码样式,包括设置多种规则如PSR1, PSR2等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

根目录 创建 .cs 并且 写入 以下内容

#!/bin/bash

vendor/bin/php-cs-fixer fix

根目录 创建 .php-cs-fixer.php 并且 写入 以下内容

<?php

declare(strict_types=1);

/*
 * This document has been generated with
 * https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0.0-rc.1|configurator
 * you can change this configuration by importing this file.
 */
$config = new PhpCsFixer\Config();
return $config
    ->setRiskyAllowed(true)
    ->setRules([
        '@PHP80Migration' => true,
        '@PHP80Migration:risky' => true,
        '@PSR1' => true,
        '@PSR2' => true,
        '@PhpCsFixer:risky' => true,
        '@PhpCsFixer' => true,
        'cast_spaces' => ['space'=>'none'],
        'date_time_immutable' => true,
        'elseif' => false,
        'final_class' => true,
        'final_public_method_for_abstract_class' => true,
        'global_namespace_import' => ['import_classes'=>true],
        'increment_style' => false,
        'mb_str_functions' => true,
        'multiline_whitespace_before_semicolons' => ['strategy'=>'new_line_for_chained_calls'],
        'native_function_invocation' => false,
        'no_break_comment' => false,
        'nullable_type_declaration_for_default_null_value' => true,
        'php_unit_dedicate_assert' => true,
        'php_unit_dedicate_assert_internal_type' => true,
        'php_unit_expectation' => true,
        'php_unit_internal_class' => false,
        'php_unit_namespaced' => true,
        'php_unit_no_expectation_annotation' => true,
        'php_unit_test_class_requires_covers' => false,
        'phpdoc_add_missing_param_annotation' => false,
        'phpdoc_align' => ['align'=>'left'],
        'yoda_style' => false,
    ])
    ->setFinder(PhpCsFixer\Finder::create()
        ->exclude('vendor')
        ->in(__DIR__)
    )
    ->setCacheFile(__DIR__.'/.php-cs-fixer.cache')
    ;

执行脚本

./.cs
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值