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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值