我有数百个可怕的缩进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
$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";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。