php缩紧空格,使用php-cs-fixer修复PHP代码文件的缩进
我有數(shù)百個可怕的縮進(jìn)PHP文件,混合標(biāo)簽和空格(甚至混合行結(jié)尾,我想)我想用php-cs-fixer v2修復(fù)它們.
我已經(jīng)根據(jù)我的需要配置了php-cs-fixer,并且相應(yīng)地擦除了代碼 – 除了縮進(jìn).我已經(jīng)嘗試了一個最小的配置,如下圖所示,以確定問題.但我無法直接得到縮進(jìn)修正器:
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中實際應(yīng)用的規(guī)則)文件如下所示:
{
"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
}
}
這里有一些非常縮進(jìn)的示例文件內(nèi)容.
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’;一個或多個前導(dǎo)標(biāo)簽/空格無法正確縮進(jìn).
我對其他方法持開放態(tài)度.其他人必須面對這樣的格式問題.
我也嘗試過NetBeans,它恰好可以很好地格式化源代碼,但手動打開每個文件并通過快捷方式應(yīng)用源格式是很繁瑣的.
解決方法:
您應(yīng)該使用大括號修復(fù)程序來強(qiáng)制縮進(jìn).
The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.
indentation_type只是強(qiáng)制執(zhí)行一致性.
但由于兩個修復(fù)程序都已包含在@ PSR2中,因此應(yīng)正確修復(fù)代碼.
請參閱README中的相關(guān)章節(jié).
使用您的代碼php-cs-fixer 2.6生成以下代碼
$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;
壓痕只是部分固定的地方.
我把它減少到下面的代碼
echo "a";
echo "b";
echo "c";
它看起來像php-cs-fixer中的一個bug.
標(biāo)簽:php,code-formatting,php-cs-fixer
來源: https://codeday.me/bug/20190622/1264120.html
總結(jié)
以上是生活随笔為你收集整理的php缩紧空格,使用php-cs-fixer修复PHP代码文件的缩进的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php扩展 zval_copy_ctor
- 下一篇: kattle的java安装,Kettle