我有几百个具有混合制表符和空格的可怕缩进PHP文件(甚至混合行结尾,我想)我想用php-cs-fixer v2 +修复它们。使用php-cs-fixer修复PHP代码文件的缩进
我已经配置了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";
& include 'connect.php';
与一个或多个龙头选项卡/空间,没有得到适当的缩进。
我接受其他方法。其他人之前必须面对这样的格式问题。
我也尝试过NetBeans,它恰好格式化源代码,但手动打开每个文件并通过快捷方式应用源格式是很繁琐的。
_Small Note_从PSR-2规范'代码必须使用4个空格进行缩进,而不是制表符。' – RiggsFolly
感谢上帝,他们不会将我们发送到使用标签的gulag。 –
感谢您的建议@RiggsFolly。将缩进更改为4个空格不会改善情况。 – wp78de