8

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

common sense
  • 3,775
  • 6
  • 22
  • 31
ramsey_lewis
  • 558
  • 8
  • 25

2 Answers2

11

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.

ponsfrilus
  • 1,010
  • 1
  • 17
  • 25
  • 2
    Very instructive thanks ! I use "short" array_syntax `[]`. PHP CS Fixer configuration is very helpful : added to bookmark – ramsey_lewis Feb 26 '19 at 11:05
  • 1
    Even this works. My god. Just to get this bid to figure out take me 20 mins. It is crazy. – Hao Jun 28 '20 at 14:36
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',
    ),
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 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 ? – ponsfrilus Jan 30 '19 at 12:15
  • 1
    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 path` – ramsey_lewis Feb 26 '19 at 10:13
  • @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. – ponsfrilus Feb 26 '19 at 10:32
  • @scrowler thanks for your answer. The link is broken do you have a new one? I could not find a place explaining all available rules. – Handsome Nerd Jul 10 '21 at 22:41
  • https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/index.rst this looks like the latest version – scrowler Jul 12 '21 at 16:55