wp7*_*8de 5 php code-formatting php-cs-fixer
我有数百个包含混合制表符和空格的可怕缩进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")
Run Code Online (Sandbox Code Playgroud)
当前,我使用以下命令在Windows框上运行此命令(此处为单个文件):
php-cs-fixer.bat fix new_user.php --config /full/windowspath/to/php_cs.dist
Run Code Online (Sandbox Code Playgroud)
以防万一,生成的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
}
}
Run Code Online (Sandbox Code Playgroud)
这是一些缩进的示例文件内容。
<?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;
Run Code Online (Sandbox Code Playgroud)
我最讨厌像$test="abc";&这样的行, include 'connect.php';并且带有一个或多个无法正确缩进的前导制表符/空格。
我愿意接受其他方法。其他人之前必须已经遇到过格式化问题。
我也尝试过NetBeans,它恰好可以很好地格式化源,但是手动打开每个文件并通过快捷方式应用源格式很繁琐。
您应该使用braces修复程序来强制缩进。
每个结构的主体必须用大括号括起来。支架应正确放置。牙套主体应适当缩进。
indentation_type只是强制执行一致性。
但由于两个修复程序都已包含在其中,@PSR2因此代码应该正确修复。
请参阅自述文件中的相关部分。
使用您的代码 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;
Run Code Online (Sandbox Code Playgroud)
其中压痕仅部分固定。
我将其简化为下面的代码
<?php
echo "a";
echo "b";
echo "c";
Run Code Online (Sandbox Code Playgroud)
它看起来像是 php-cs-fixer 中的错误。
| 归档时间: |
|
| 查看次数: |
3740 次 |
| 最近记录: |