【问题标题】:Fix indentation of PHP code files with php-cs-fixer使用 php-cs-fixer 修复 PHP 代码文件的缩进
【发布时间】:2018-02-28 12:59:44
【问题描述】:

我有数百个带有混合制表符和空格的可怕缩进 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";include 'connect.php'; 这样的行,其中一个或多个前导制表符/空格没有正确缩进。

我对替代方法持开放态度。其他人以前一定遇到过这样的格式问题。

我也尝试过 NetBeans,它恰好可以很好地格式化源代码,但是手动打开每个文件并通过快捷方式应用源代码格式很繁琐。

【问题讨论】:

  • 小注来自 PSR-2 规范 Code MUST use 4 spaces for indenting, not tabs.
  • 感谢上帝,他们还没有派我们去古拉格使用标签。
  • 感谢您的建议@RiggsFolly。将缩进更改为 4 个空格并不能改善这种情况。
  • 旧的 php-cs-fixer 无法修复 cmets 后的缩进,可能还有其他错误。

标签: php code-formatting php-cs-fixer


【解决方案1】:

OP 说

我对替代方法持开放态度。其他人一定遇到过这样的格式问题。

我们的PHP Formatter 可以很好地缩进文件。请参阅 PHP Formatter 处理的 OP 的“严重缩进”示例:

C:\>DMSFormat PHP~v7 \temp\test.php
PHP~v7 PrettyPrinter Version 1.3.17
Copyright (C) 2004-2016 Semantic Designs, Inc; All Rights Reserved; SD Confidential
Powered by DMS (R) Software Reengineering Toolkit
DMS_PHP~v7_INPUT_ENCODING=ISO-8859-1
DMS_PHP~v7_OUTPUT_ENCODING=ISO-8859-1
Parsing \temp\test.php [encoding ISO-8859-1 +CRLF +LF +CR +NEL +1 /^I]


<?php
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;

(我必须添加

<?php

到文件的开头使其合法。)

此示例从文件运行到控制台。您还可以对一个文件执行一个文件,或者使用项目文件运行整个文件列表[这可能是 OP 想要的]。

PHP 格式化程序使用真正的 PHP 解析器来处理源文本并构建抽象语法树,并使用特殊的漂亮打印机将 AST 打印回格式良好的文本。 它不能搞砸文件。

【讨论】:

    【解决方案2】:

    关于替代选项。我在 Visual Studio Code 中也遇到了自动代码格式化的问题。我尝试了一些格式化程序,但只有phpfmt 解决了我的缩进问题并将大括号放在正确的位置。它也有许多自定义选项,但我没有测试它们,因为它们不是必需的。

    【讨论】:

      【解决方案3】:

      我将根据导致我解决问题的发现回答我自己的问题。

      虽然格式基本有效,但对我来说最重要的是缩进。如果有一些前导空格或制表符,则修复后某些行会一直突出。

      由于 php-cs-fixer 和 phpcbf 都无法正确修复缩进,我采取了绝望的措施,并在如下脚本中使用 sed 修剪每行中的每个前导空格作为准备步骤:

      sed "s/^[ \t]*//" -i test.php
      

      然后我再次使用 php-cs-fixer 和 phpcbf 处理了一些准备好的文件,以找出根据 PSR-2 格式化文件的效果更好的文件。这是可耻的,但两个修复程序再次失败 - 现在显示出一些不同的缺点(即错误)。长话短说,我终于了解到将这两个工具结合起来可以生成格式正确的代码文件。真是一团糟。

      所以,在 sed 之后,我运行 phpcbf

      phpcbf --standard="PSR2" test.php
      

      紧随其后

      php-cs-fixer fix test.php --rules=@PSR2
      

      突然间,我有了精美的 PSR-2 格式的 PHP 文件。不是最有效的方法,但它确实有效。

      一些额外的 cmets:

      • 如果您想应用额外的修复程序规则,我建议您在第四步中使用 PSR-2 基线格式中不同的、更完整的 php_cs 配置(因为,您知道,还有更多的修复程序问题。 .).
      • 根据 PSR-2 的要求,我建议使用 4 个空格作为缩进。根据我的经验,如果您坚持使用标签,事情会变得更加复杂。
      • 如果 php-cs-fixer 和 phpcbf 没有这么多问题,则不需要描述的过程。我会一一汇报,希望以后也能一口气做到。

      【讨论】:

      【解决方案4】:

      你应该使用braces fixer 来强制缩进。

      每个结构的主体必须用大括号括起来。应正确放置大括号。 大括号主体应适当缩进。

      indentation_type 只是强制执行一致性。

      但是由于这两个修复程序都已经包含在@PSR2 中,所以应该正确修复代码。

      查看README中的相关部分。


      使用您的代码 php-cs-fixer 2.6 生成以下代码

      <?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";
      

      它看起来像 php-cs-fixer 中的一个错误。

      【讨论】:

      • 谢谢@Jan Tojnar。我的完整配置中有一个大括号规则,但是当我开始探测时,它被条带化了。再次添加规则。不幸的是,情况并没有改善。
      • 除了braces 固定器,您应该使用array_indentation 固定器来正确缩进数组。
      猜你喜欢
      • 2017-08-08
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      相关资源
      最近更新 更多