php缩紧空格,使用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
}
}
這里有一些非常縮進的示例文件內容.
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生成以下代碼
$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.
標簽:php,code-formatting,php-cs-fixer
來源: https://codeday.me/bug/20190622/1264120.html
總結
以上是生活随笔為你收集整理的php缩紧空格,使用php-cs-fixer修复PHP代码文件的缩进的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php扩展 zval_copy_ctor
- 下一篇: kattle的java安装,Kettle