【问题标题】:Use tab for indentation in PHP-CS-Fixer在 PHP-CS-Fixer 中使用制表符进行缩进
【发布时间】:2017-08-08 12:52:56
【问题描述】:

如何配置 PHP-CS-Fixer 以使用制表符进行缩进?

我可以看到indentation_type 修复程序

* indentation_type [@PSR2, @Symfony]
  | Code MUST use configured indentation type.

但是如何配置缩进类型呢?如果我尝试设置'indentation_type' => 'tab',,我会收到错误

[indentation_type] Is not configurable.

【问题讨论】:

    标签: php php-cs-fixer


    【解决方案1】:

    它在文档中,有些我错过了它(可能是因为我正在搜索术语“tab”而不是“indentation”)

    对于其他寻找相同方法的人,PhpCsFixer\Config 中有一个 setIndent 方法。

    return PhpCsFixer\Config::create()
        ->setRules([
            '@PSR2' => true,
            'indentation_type' => true,
        ])
        ->setIndent("\t")
        ->setLineEnding("\n")
        ->setFinder($finder)
    

    【讨论】:

    • 拥有这些数组并且没有关于所有规则的好的文档真的很烦人......像 ->useTabsForIndent() 这样的方法将更具描述性并且很容易发现任何提供自动完成的想法。
    • 尽管使用单一方法会很好,但我认为这只是一个小小的不便。由于这是一个项目的一次性配置,并且在各个项目中几乎总是相同的,我不认为 IDE 自动完成是一个巨大的优势。
    【解决方案2】:

    首先在您的项目根目录中创建.php_cs 文件。然后将以下行添加到.php_cs 文件中

    <?php
    return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'indentation_type' => true,
    ])
    ->setIndent("\t")
    ->setLineEnding("\n")
    ->setFinder($finder)
    

    然后运行以下命令来解决问题

    vendor/bin/php-cs-fixer fix . --config .php_cs
    

    【讨论】:

    • 该问题询问使用tab 进行缩进,您的回答说明了如何使用space
    • @JoyceBabu 感谢您的评论,我已经更新了标签的代码。
    • 现在这是现有答案的副本。什么是增值?
    • 如果我们必须解决问题,我们需要代码以及解决问题的命令。
    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 2019-12-07
    • 2018-02-11
    • 1970-01-01
    • 2011-10-24
    • 2023-04-08
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多