Skip to content

Keep empty line breaks inside control statements #4487

@felixpenrose

Description

@felixpenrose

In the following code examples below, I'm intentionally adding a line break before and after an if/else generally to improve readability, however these get removed. Is it possible to stop this from happening?

In some cases, I agree with the fix applied, but more often than not, there are smaller if/elseif/else blocks which are harder to read when there's no empty line breaks in between the control statement and the code within.

The PHP version you are using ($ php -v):

PHP 7.2.0 (cli) (built: Nov 28 2017 23:48:49) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.15.1 Europe Round by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

VSCode php-cs-fixer
https://marketplace.visualstudio.com/items?itemName=junstyle.php-cs-fixer

The configuration file you are using, if any:

<?php

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2'                        => true,
        'array_indentation'            => true,
        'array_syntax'                 => ['syntax' => 'short'],
        'combine_consecutive_unsets'   => true,
        'method_separation'            => false,
        'single_quote'                 => true,
        'full_opening_tag'             => true,
        'fully_qualified_strict_types' => true,

        'no_multiline_whitespace_before_semicolons' => true,

        'binary_operator_spaces' => [
            'align_double_arrow' => true,
            'align_equals'       => true,
        ],
        'braces' => [
            'allow_single_line_closure' => true,
        ],
        'cast_spaces'                      => true,
        'class_definition'                 => ['singleLine' => true],
        'concat_space'                     => ['spacing' => 'one'],
        'declare_equal_normalize'          => ['space'   => 'single'],
        'function_typehint_space'          => true,
        'hash_to_slash_comment'            => true,
        'include'                          => false,
        'lowercase_cast'                   => true,
        'lowercase_constants'              => true,
        'lowercase_keywords'               => true,
        'lowercase_static_reference'       => true,
        'semicolon_after_instruction'      => true,
        'ternary_to_null_coalescing'       => true,
        'no_extra_consecutive_blank_lines' => [
            'return',
        ],
        'no_extra_blank_lines' => [
            'return',
        ],
        'no_multiline_whitespace_around_double_arrow' => true,
        'no_spaces_around_offset'                     => true,
        'no_unused_imports'                           => true,
        'no_whitespace_before_comma_in_array'         => true,
        'no_whitespace_in_blank_line'                 => false,
        'object_operator_without_whitespace'          => true,
        'single_blank_line_before_namespace'          => true,
        'ternary_operator_spaces'                     => true,
        'trailing_comma_in_multiline_array'           => true,
        'trim_array_spaces'                           => true,
        'unary_operator_spaces'                       => true,
        'whitespace_after_comma_in_array'             => true,
        'elseif'                                      => true,
    ])
    ->setLineEnding("\n");

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
if ($foo == true)
{
    
    echo 'Hi';

}
else if ($bar  == 'bar')
{

    echo 'foo';

} else {

    echo 'bar';

    echo 'baz';

}
  • with unexpected changes applied when running PHP CS Fixer:
if ($foo == true) {
    echo 'Hi';
} elseif ($bar == 'bar') {
    echo 'foo';
} else {
    echo 'bar';

    echo 'baz';
}
  • with the changes you expected instead:
if ($foo == true) {
    
    echo 'Hi';

} elseif ($bar  == 'bar') {

    echo 'foo';

} else {

    echo 'bar';

    echo 'baz';

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions