php-cs-fixer:保持括号在函数声明的同一行

时间:2018-11-16 11:01:31

标签: php php-cs-fixer

Php CS修复程序正在执行

function foobar()
{
....
}

我想要:

function foobar() {
....
}

我看不到在我的配置.php_cs文件的同一行或https://github.com/FriendsOfPHP/PHP-CS-Fixer上将大括号保持在同一行的配置是什么。我正在使用php-cs-fixerV2。

我的配置文件:https://pastebin.com/v03v9Lb5

2 个答案:

答案 0 :(得分:2)

您已启用PSR-2,这需要在下一行使用大括号。在the documentation中,您似乎可以将braces.position_after_functions_and_oop_constructs设置为same(默认为next):

  
      
  • position_after_functions_and_oop_constructs ('next', 'same'):是否应在经典构造(非匿名类,接口,特征,方法和非lambda函数)之后将开括号放在“下一个”或“相同”行上;默认为“下一个”
  •   

myconfig.php_cs:

    'braces' => array(
        'allow_single_line_closure' => true,
        'position_after_functions_and_oop_constructs' => 'same',
    ),

答案 1 :(得分:1)

您在此处描述的样式称为“ the one true brace style”(缩写为1TBS或OTBS)。

当我遇到完全相同的问题时,我终于在这里结束了,尽管@Robbie的答案有所帮助,但我仍然需要进行大量搜索。

所以我终于在存储库中得到了这个.php_cs

<?php

$finder = PhpCsFixer\Finder::create()
    //->exclude('somedir')
    //->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php'
    ->in(__DIR__)
;

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'strict_param' => false,
        'array_syntax' => ['syntax' => 'long'],
        'braces' => [
            'allow_single_line_closure' => true, 
            'position_after_functions_and_oop_constructs' => 'same'],
    ])
    ->setFinder($finder)
;

一些说明(来自(PHP-CS-Fixer README):

  • array_syntax 长表示array(),而不是[]。是否使用长数组或短数组语法;默认为'long';
  • allow_single_line_closure :是否应允许使用单行lambda表示法;默认为false;
  • position_after_functions_and_oop_constructs :是否应在经典构造(非匿名类,接口,特征,方法和非lambda函数)之后将开括号放在“下一个”或“相同”行上;默认为“下一个”。

在诸如Atom这样的IDE中,php-cs-fixer plugin将在当前项目的根路径中搜索.php_cs配置文件。也可以指定路径。

最后但并非最不重要的是,Michele LocatiPHP CS Fixer configuration的网站确实可以提供帮助。