Php-cs-fixer: Do not add new line between empty scope/curly brackets

Created on 23 Jan 2020  路  9Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

Currently the following code:

$x = new class() {};

is normalized to:

$x = new class() {
};

Config file:

$finder = PhpCsFixer\Finder::create()
    ->in([__DIR__])
    ->exclude([
        'cache',
        'vendor',
    ]);

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@Symfony' => true,
        '@Symfony:risky' => true,
        '@PHP71Migration' => true,
        'concat_space' => [
            'spacing' => 'one',
        ],
        'no_blank_lines_after_phpdoc' => false,
        'no_superfluous_phpdoc_tags' => false,
        'phpdoc_align' => [
            'align' => 'left',
        ],
        'phpdoc_separation' => false,
        'phpdoc_summary' => false,
        'single_line_throw' => false,
        'yoda_style' => [
            'equal' => false,
            'identical' => false,
        ],
        'native_function_invocation' => false,
        'non_printable_character' => [
            'use_escape_sequences_in_strings' => true,
        ],
        'psr4' => false,
        'array_indentation' => true,
        'no_null_property_initialization' => true,
        'pow_to_exponentiation' => true,
        'combine_nested_dirname' => true,
    ])
    ->setFinder($finder)
    ->setCacheFile(__DIR__ . '/cache/.php_cs.cache');

Is it possible to leave the code unchanged (i.e. without new line between {}) by the latest CS fixer version and how to change the config above?

kinfeature request topiPSR-12

Most helpful comment

maybe best to discuss on the PSR12 mailing list?

https://groups.google.com/forum/?fromgroups#!topic/php-fig/VWcw3NnNHnM posted to the mailing list, please continue the discussion there

All 9 comments

I think this is done/controlled by the braces fixer, you can try the configuration option position_after_anonymous_constructs => same to see if it helps (the README lists more options to try, grap for braces)

@SpacePossum That controls the position of opening brace.

The same issue has the try/catch code with empty catch:

try {
    test();
} catch (\Exception $e) {}

which is currently normalized to:

try {
    test();
} catch (\Exception $e) {
}

wdyt about this issue, does it deserve a special option and can you advise which fixer code to fix? (as this option should work across all language constructs with empty bodies defined by curly braces) Thanks.

:+1: for allowing single line anonymous classes, like we do for allow_single_line_closure.

I'm not sure about the other cases though. The braces fixer is already a monster-fixer with a lot of complexity so to prevent an even higher maintenance cost I'm not in favor if adding a lot of new features there at the moment.

@Jean85 Wdyt about:

$x = new class() {};

vs:

$x = new class() {
};

is this defined by any PHP-FIG RFC? Should a new line / space / empty string be preffered between { and } if there is no expression / comment inside?

PSR-12 does not explicitly mentions this case, but the code example has it, and it's in the first form, with no new lines.

@Jean85 Thanks, shouldn't it be added? There are many empty block code usages like:

class A extends B
{}

try {
    // try body
} catch (FirstThrowableType $e) {}

For me, the best rule to me seems add a blank line before the closing bracket if and only if a new line is before the opening bracket, i.e.:

class A extends B
{
}

try {
    // try body
} catch (FirstThrowableType $e) {}

maybe best to discuss on the PSR12 mailing list?

maybe best to discuss on the PSR12 mailing list?

https://groups.google.com/forum/?fromgroups#!topic/php-fig/VWcw3NnNHnM posted to the mailing list, please continue the discussion there

馃憤 for allowing single line anonymous classes

@SpacePossum like this https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4012 ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amitbisht511 picture amitbisht511  路  3Comments

BackEndTea picture BackEndTea  路  3Comments

sennewood picture sennewood  路  3Comments

Bilge picture Bilge  路  3Comments

OskarStark picture OskarStark  路  3Comments