Php-cs-fixer: Fixers "braces" change block else-if

Created on 13 Feb 2018  路  7Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

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

PHP 7.1.11 (cli) (built: Nov 28 2017 16:16:51) ( NTS )

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

PHP CS Fixer version 2.2.16

The command you use to run PHP CS Fixer:

php bin/php-cs-fixer fix --config=.php_cs.php --verbose --show-progress=estimating

The configuration file you are using, if any:

<?php
return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR1' => true,
        'encoding' => true,
        'full_opening_tag' => true,
        'blank_line_after_namespace' => true,
        'braces' => [
            'allow_single_line_closure' => true,
            'position_after_functions_and_oop_constructs' => 'next',
        ],
        'class_definition' => true,
        'elseif' => false,
        'function_declaration' => true,
        'indentation_type' => true,
        'line_ending' => true,
        'lowercase_constants' => true,
        'lowercase_keywords' => true,
        'method_argument_space' => true,
        'no_closing_tag' => true,
        'no_spaces_after_function_name' => true,
        'no_spaces_inside_parenthesis' => true,
        'no_trailing_whitespace' => true,
        'no_trailing_whitespace_in_comment' => true,
        'single_blank_line_at_eof' => true,
        'single_class_element_per_statement' => true,
        'single_import_per_statement' => true,
        'single_line_after_imports' => true,
        'switch_case_semicolon_to_colon' => true,
        'switch_case_space' => true,
        'visibility_required' => true,
        'ternary_to_null_coalescing' => true,
        'declare_equal_normalize' => true,
        'function_typehint_space' => true,
        'lowercase_cast' => true,
        'magic_constant_casing' => true,
        'native_function_casing' => true,
        'no_empty_comment' => true,
        'no_empty_phpdoc' => true,
        'no_leading_namespace_whitespace' => true,
        'no_mixed_echo_print' => true,
        'no_short_bool_cast' => true,
        'no_spaces_around_offset' => true,
        'no_trailing_comma_in_list_call' => true,
        'php_unit_fqcn_annotation' => true,
        'phpdoc_inline_tag' => true,
        'phpdoc_no_access' => true,
        'phpdoc_no_useless_inheritdoc' => true,
        'phpdoc_return_self_reference' => true,
        'protected_to_private' => true,
    ])
    ->setFinder(PhpCsFixer\Finder::create()
        ->exclude('vendor')
        ->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')
    )
    ;

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

  • before running PHP CS Fixer (no changes):
        } else if ($isFoo) {
            // Something
        }
        } else if ($isFoo) {
            // Something
        } else if($isBar) {
            // Something else
        }
  • with unexpected changes applied when running PHP CS Fixer:
            } else {
                if ($isFoo) {
                     // Something
                  }
            }
        } else {
            if ($isFoo) {
                // Something
            } else {
                if($isBar) {
                    // Something else
                }
            }
        }
  • with the changes you expected instead:
        } else if ($isFoo) {
            // Something
        }
        } else if ($isFoo) {
            // Something
        } else if ($isBar) {
            // Something else
        }

This issue happen when fixer braces was enabled.

kinquestion

All 7 comments

This is expected, as after else you shall create a code block with braces.
It's the same situation as

} else while (condition()) {
    call();
}

vs

} else {
    while (condition()) {
        call();
    }
}

you would use only later form, won't you?


If you don't like change you are experiencing, use elseif rule to convert else if into elseif as addition to braces rule

closing as "answered", let us know if you want to know more @pudovmaxim

I hit something like this today.

It's the same situation as } else while(){}

I do not agree. The reason is that else while() {} can only be the very last thing, since it doesn't chain. With if, you can always another else if. Having the code fixer force else if into elseif seems like an odd workaround. Is there no other configuration for this?

I wish too that else if is not replaced. I mean, else if and elseif have the same behavior, and I see a lot of code written with else if. Else if is the common form acceptable in many languages, we should not be forced to use elseif.

The project scope is to provide a tool that is extendable with your own custom written rules and to provide rules for standards and code styles accepted by big open source communities. Its scope is _not_ to be a general code style changer with everyone's preferred code style supported out of the box. It would be undermine what with try to do; promote standards, and we simply cannot develop and maintain everyone's preferred code style.

So; you are not forced into anything by someone here as far as I know; you can disable the rule(s) or not use this tool all together, you can also write your own rule(s) and use those as a replacement of the ones you don't like, you can also fork the project. Again, no one here is forcing you to use this tool.
But please let me know if someone is forcing you here into something.

There is a separate rule to change else if into elseif. I don't see anyone wanting to change chained else if into the nested version (breaks indentation). That being said, I agree that PSR2 promotes elseif version

beauty of opensource - go ahead and raise a PR with your proposal to make braces fixer more configurable, @nmoreaud

Was this page helpful?
0 / 5 - 0 ratings