php-cs-fixer : keep brace on the same line of function declaration
Asked Answered
D

2

9

Php cs fixer is doing :

function foobar()
{
....
}

and I want:

function foobar() {
....
}

I can't see what is the config to keep braces on the same line in my config .php_cs file, nor on https://github.com/FriendsOfPHP/PHP-CS-Fixer. I'm using php-cs-fixerV2.

My config file: https://pastebin.com/v03v9Lb5

Dissimilar answered 16/11, 2018 at 11:1 Comment(0)
E
9

You have PSR-2 enabled, which requires the braces on the next line. From the documentation it looks like you can set braces.position_after_functions_and_oop_constructs to same (default would be next):

  • position_after_functions_and_oop_constructs ('next', 'same'): whether the opening brace should be placed on “next” or “same” line after classy constructs (non-anonymous classes, interfaces, traits, methods and non-lambda functions); defaults to 'next'

myconfig.php_cs:

    'braces' => array(
        'allow_single_line_closure' => true,
        'position_after_functions_and_oop_constructs' => 'same',
    ),
Eulogia answered 16/11, 2018 at 11:7 Comment(5)
I'm also looking for the 1TBS mode for cs-fixer but I'm using Atom and the packages atom-beautify & php-cs-fixer. Does anyone know it this configuration can be put in the atom config file, the packages settings, or elsewhere ?Bombardier
hello @ponsfrilus. I never received notification for your coment sorry. you can add this in atom. File > setting > packages > php-cs-fixer, then add your file's path in PHP-CS-fixer config file pathDissimilar
@user2203384 thanks, I've ended with a .php_cs in my repo, as this file is loaded by default in the php-cs-fixer in atom.Bombardier
@Eulogia thanks for your answer. The link is broken do you have a new one? I could not find a place explaining all available rules.Matsu
github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/… this looks like the latest versionEulogia
B
12

The style you described here is called "the one true brace style" (abbreviated as 1TBS or OTBS).

As I get the exact same problem, I've finally ended here and while @Robbie answer helps, I still needed to search a lot.

So I finally get this .php_cs in my repository:

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

Some explainations (from the (PHP-CS-Fixer README):

  • array_syntax to long means array() instead of []. Whether to use the long or short array syntax; defaults to 'long';
  • allow_single_line_closure: whether single line lambda notation should be allowed; defaults to false;
  • position_after_functions_and_oop_constructs: whether the opening brace should be placed on "next" or "same" line after classy constructs (non-anonymous classes, interfaces, traits, methods and non-lambda functions); defaults to 'next'.

In IDE such Atom, the php-cs-fixer plugin will search for the .php_cs config file in the root path of the current project. It's also possible to specify the path.

Last but not least, the website of Michele Locati, PHP CS Fixer configuration can really helps.

Bombardier answered 26/2, 2019 at 10:48 Comment(2)
Very instructive thanks ! I use "short" array_syntax []. PHP CS Fixer configuration is very helpful : added to bookmarkDissimilar
Even this works. My god. Just to get this bid to figure out take me 20 mins. It is crazy.Duplessismornay
E
9

You have PSR-2 enabled, which requires the braces on the next line. From the documentation it looks like you can set braces.position_after_functions_and_oop_constructs to same (default would be next):

  • position_after_functions_and_oop_constructs ('next', 'same'): whether the opening brace should be placed on “next” or “same” line after classy constructs (non-anonymous classes, interfaces, traits, methods and non-lambda functions); defaults to 'next'

myconfig.php_cs:

    'braces' => array(
        'allow_single_line_closure' => true,
        'position_after_functions_and_oop_constructs' => 'same',
    ),
Eulogia answered 16/11, 2018 at 11:7 Comment(5)
I'm also looking for the 1TBS mode for cs-fixer but I'm using Atom and the packages atom-beautify & php-cs-fixer. Does anyone know it this configuration can be put in the atom config file, the packages settings, or elsewhere ?Bombardier
hello @ponsfrilus. I never received notification for your coment sorry. you can add this in atom. File > setting > packages > php-cs-fixer, then add your file's path in PHP-CS-fixer config file pathDissimilar
@user2203384 thanks, I've ended with a .php_cs in my repo, as this file is loaded by default in the php-cs-fixer in atom.Bombardier
@Eulogia thanks for your answer. The link is broken do you have a new one? I could not find a place explaining all available rules.Matsu
github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/… this looks like the latest versionEulogia

© 2022 - 2024 — McMap. All rights reserved.