4

I've started to use PHP-CS-Fixer and I was wondering if there is any configuration to do not automatically add curly braces on inline statements, for example

//I want to keep this way
if (empty($var))
    throw new Exception('An error message..')

but when I format it turns into this:

if (empty($var)) {
    throw new Exception('An error message..')
}

My php_cs.dist config:

...
->setRules([
          'no_unneeded_curly_braces' => true,
          'braces' => [
            'allow_single_line_closure' => true,
            'position_after_functions_and_oop_constructs' => 'same'
          ]
    ])
...

Is this possible to achieve with PHP-CS-Fixer?

Fantasmic
  • 1,122
  • 17
  • 25
  • I understand that's precisely what the `braces` rule does. Are you setting it only to control the position of existing braces? – Álvaro González Dec 05 '19 at 18:27
  • 1
    @ÁlvaroGonzález I'm using "position_after_functions_and_oop_constructs" to keep class and functions declarations braces on the same line, but it shouldn't interfere other statements like if/else. So yes, I just want it to keep on the same line when there is already an existing brace. – Fantasmic Dec 05 '19 at 18:33
  • @ÁlvaroGonzález do you know if this is possible? – Fantasmic Dec 05 '19 at 18:40
  • 1
    No, sorry. Otherwise I'd have posted an answer ;-) – Álvaro González Dec 05 '19 at 18:41
  • @ÁlvaroGonzález can I ask you one more thing? :D Do you know if there is an option to automatically add an empty line after class body? – Fantasmic Dec 05 '19 at 18:49
  • Do you mean before the closing brace? I may be wrong but I think there isn't. Fixers are more focused on commonly used styles like PSR-2. – Álvaro González Dec 05 '19 at 18:59
  • @ÁlvaroGonzález I mean the line after class declaration and before closing class braces – Fantasmic Dec 05 '19 at 19:21
  • Read this: https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/2780 – Movahhedi Jun 30 '22 at 07:35

0 Answers0