使用php-cs-fixer修复PHP代码文件的缩进

我有数百个可怕的缩进PHP文件,混合标签和空格(甚至混合行结尾,我想)我想用php-cs-fixer v2修复它们.

我已经根据我的需要配置了PHP-cs-fixer,并且相应地擦除了代码 – 除了缩进.我已经尝试了一个最小的配置,如下图所示,以确定问题.但我无法直接得到缩进修正器:

return PHPCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'indentation_type' => true,
        'braces' => ['position_after_functions_and_oop_constructs' => 'same'],
    ])
    ->setIndent("\t")
    ->setLineEnding("\r\n")

目前,我使用以下命令在Windows框中运行此命令(此处为单个文件):

PHP-cs-fixer.bat fix new_user.PHP --config /full/windowspath/to/PHP_cs.dist

以防万一,生成PHP_cs.cache(其中包含JSON中实际应用的规则)文件如下所示:

{
    "PHP": "5.6.31",
    "version": "2.6.0:v2.6.0#5642a36a60c11cdd01488d192541a89bb44a4abf",
    "rules": {
        "blank_line_after_namespace": true,
        "braces": {
            "position_after_functions_and_oop_constructs": "same"
        },
        "class_deFinition": true,
        "elseif": true,
        "function_declaration": true,
        "indentation_type": true,
        "line_ending": true,
        "lowercase_constants": true,
        "lowercase_keywords": true,
        "method_argument_space": {
            "ensure_fully_multiline": true
        },
        "no_break_comment": true,
        "no_closing_tag": true,
        "no_spaces_after_function_name": true,
        "no_spaces_inside_parenthesis": true,
        "no_trailing_whitespace": true,
        "no_trailing_whitespace_in_comment": true,
        "single_blank_line_at_eof": true,
        "single_class_element_per_statement": {
            "elements": ["property"]
        },
        "single_import_per_statement": true,
        "single_line_after_imports": true,
        "switch_case_semicolon_to_colon": true,
        "switch_case_space": true,
        "visibility_required": true,
        "encoding": true,
        "full_opening_tag": true
    },
    "hashes": {
        "new_students.org_.PHP": -151826318
    }
}

这里有一些非常缩进的示例文件内容.

<?PHP
session_start();

  include 'connect.PHP';
include 'functions.PHP';    

$test= "abc";
    $additional_studs = "";
    if (date('m') == 12 and $term='SP') {
        $yr_suffix = date('y') + 1;
    } else {
        $yr_suffix = date('y');
    }


    function dup_stud($id, $conn)
    {//...
}

$i = 0;

我最烦恼的是像$test =“abc”这样的行; &安培;包括’connect.PHP’;一个或多个前导标签/空格无法正确缩进.

我对其他方法持开放态度.其他人必须面对这样的格式问题.

我也尝试过NetBeans,它恰好可以很好地格式化源代码,但手动打开每个文件并通过快捷方式应用源格式是很繁琐的.

解决方法:

您应该使用大括号修复程序来强制缩进.

The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.

indentation_type只是强制执行一致性.

但由于两个修复程序都已包含在@ PSR2中,因此应正确修复代码.

请参阅README中的相关章节.

使用您的代码PHP-cs-fixer 2.6生成以下代码

<?PHP
$test= "abc";
    $additional_studs = "";
    if (date('m') == 12 and $term='SP') {
        $yr_suffix = date('y') + 1;
    } else {
        $yr_suffix = date('y');
    }


    function dup_stud($id, $conn)
    {//...
    }

$i = 0;

压痕只是部分固定的地方.

我把它减少到下面的代码

<?PHP
echo "a";
    echo "b";
echo "c";

它看起来像PHP-cs-fixer中的一个bug.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...