blog and things oh hi there!
Upgrading to PHP CS Fixer 2

If you have not used it yet, PHP CS Fixer is a tool to automatically fix your PHP code based on different coding standards. You can use grouped rulesets such as PSR2 or you can pick individual rules to make sure your code looks the way you want them to look.

Before version 2.0.0, you could list all your rules separated by a comma like this:

php-cs-fixer fix code.php --rules=line_ending,full_opening_tag,indentation_type

but with v2.0.0, some of these rules are now configureable and currently, you can't pass the configuration options through the command line. Instead a configuration file named .php_cs is used.

Here is what my .php_cs file looks like:

<?php
    $finder = PhpCsFixer\Finder::create()
        ->in(__DIR__);

    return PhpCsFixer\Config::create()
        ->setRules([
            '@PSR2' => true,
            'blank_line_after_opening_tag' => true,
            'array_syntax' => ['syntax' => 'short'],
            'no_short_echo_tag' => true,
            'no_unused_imports' => true,
            'standardize_not_equals' => true,
            'single_quote' => true,
            'ordered_imports' => true,
            'normalize_index_brace' => true,
            'not_operator_with_successor_space' => true,
            'no_blank_lines_after_phpdoc' => true,
            'no_blank_lines_after_class_opening' => true,
            'method_argument_space' => true,
            'linebreak_after_opening_tag' => true,
            'hash_to_slash_comment' => true,
            'function_typehint_space' => true,
            'full_opening_tag' => true,
            'concat_space' => ['spacing' => 'none'],
            'binary_operator_spaces' => [
                'align_double_arrow' => true,
                'align_equals' => false,
            ],
            'blank_line_before_return' => true,
            'no_empty_comment' => true,
            'no_leading_import_slash' => true,
            'no_leading_namespace_whitespace' => true,
            'no_mixed_echo_print' => ['use' => 'echo'],
            'no_short_bool_cast' => true,
            'ternary_operator_spaces' => true,
            'trailing_comma_in_multiline_array' => true,
        ]);

As you can tell, most of them are just boolean values that simply toggles the given rule. However, if you look at the rule array_syntax you can see that it takes an array as the value and through this, you can configure a given rule.

 'array_syntax' => ['syntax' => 'short'],

Once you have your configuration file set up the way you want, you can trigger the fixer by running:

php-cs-fixer fix code.php --config=/path/to/.php_cs
© 2019 Kevin Jung