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

您在这里描述的样式被称为"一个真正的大括号样式"(缩写为1TBS或OTBS(。

由于我遇到了完全相同的问题,我终于在这里结束了,虽然@Robbie的回答有帮助,但我仍然需要搜索很多。

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

<?php
$finder = PhpCsFixerFinder::create()
//->exclude('somedir')
//->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php'
->in(__DIR__)
;
return PhpCsFixerConfig::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自述(:

  • array_syntax到long表示array()而不是[]。是使用长数组语法还是短数组语法;默认为"long">
  • allow_single_line_closure:是否应允许使用单行lambda表示法;默认为false
  • position_after_functions_and_oop_constructs:在类构造(非匿名类、接口、特征、方法和非lambda函数(之后,左大括号应该放在"下一行"还是"同一行";默认为"next">

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

最后但并非最不重要的是,Michele Locati的网站,PHP CS Fixer的配置真的很有帮助。

您启用了PSR-2,这需要在下一行使用大括号。从文档中可以将braces.position_after_functions_and_oop_constructs设置为same(默认为next(:

  • position_after_functions_and_oop_constructs ('next', 'same'):在类构造(非匿名类、接口、特征、方法和非lambda函数(之后,左大括号应该放在"下一行"还是"同一行";默认为"next">

myconfig.php_cs:

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

最新更新