Skip to content

Instantly share code, notes, and snippets.

@petericebear
Last active June 26, 2023 02:32
Show Gist options
  • Star 59 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save petericebear/72e2b462f59b305c551c to your computer and use it in GitHub Desktop.
Save petericebear/72e2b462f59b305c551c to your computer and use it in GitHub Desktop.
Laravel 5.x php-cs-fixer config file
<?php
$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/cache')
->notPath('storage')
->notPath('vendor')
->in(__DIR__)
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$fixers = [
'-psr0',
//'align_double_arrow',
'binary_operator_spaces',
'blank_line_after_namespace',
'blank_line_after_opening_tag',
'blank_line_before_return',
'braces',
'cast_spaces',
'class_definition',
'concat_without_spaces',
'elseif',
'encoding',
'full_opening_tag',
'function_declaration',
'function_typehint_space',
'hash_to_slash_comment',
'heredoc_to_nowdoc',
'include',
'lowercase_cast',
'lowercase_constants',
'lowercase_keywords',
'method_argument_space',
'method_separation',
'native_function_casing',
'new_with_braces',
'no_alias_functions',
'no_blank_lines_after_class_opening',
'no_blank_lines_after_phpdoc',
'no_blank_lines_between_uses',
'no_closing_tag',
'no_duplicate_semicolons',
'no_empty_phpdoc',
'no_extra_consecutive_blank_lines',
'no_leading_import_slash',
'no_leading_namespace_whitespace',
'no_multiline_whitespace_around_double_arrow',
'no_multiline_whitespace_before_semicolons',
'no_short_bool_cast',
'no_singleline_whitespace_before_semicolons',
'no_spaces_after_function_name',
'no_spaces_inside_parenthesis',
'no_tab_indentation',
'no_trailing_comma_in_list_call',
'no_trailing_comma_in_singleline_array',
'no_trailing_whitespace',
'no_trailing_whitespace_in_comment',
'no_unneeded_control_parentheses',
'no_unreachable_default_argument_value',
'no_unused_imports',
'no_useless_return',
'no_whitespace_before_comma_in_array',
'no_whitespace_in_blank_lines',
'not_operator_with_successor_space',
'object_operator_without_whitespace',
'ordered_imports',
'phpdoc_align',
'phpdoc_indent',
'phpdoc_inline_tag',
'phpdoc_no_access',
'phpdoc_no_package',
'phpdoc_order',
'phpdoc_params',
'phpdoc_scalar',
'phpdoc_separation',
'phpdoc_short_description',
'phpdoc_summary',
'phpdoc_to_comment',
'phpdoc_trim',
'phpdoc_type_to_var',
'phpdoc_types',
'phpdoc_var_without_name',
'print_to_echo',
'psr4',
'self_accessor',
'short_array_syntax',
'short_scalar_cast',
'simplified_null_return',
'single_blank_line_at_eof',
'single_blank_line_before_namespace',
'single_import_per_statement',
'single_line_after_imports',
'single_quote',
'space_after_semicolon',
'standardize_not_equals',
'switch_case_semicolon_to_colon',
'switch_case_space',
'ternary_operator_spaces',
'trailing_comma_in_multiline_array',
'trim_array_spaces',
'unalign_equals',
'unary_operator_spaces',
'unix_line_endings',
'visibility_required',
'whitespace_after_comma_in_array',
];
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers($fixers)
->finder($finder)
->setUsingCache(false);
@petericebear
Copy link
Author

How to use:

  1. Install PHP-CS-Fixer from http://get.sensiolabs.org/php-cs-fixer.phar
  2. Copy file on your root project folder.
  3. From this folder, run: php php-cs-fixer.phar --verbose fix --config-file=.php_cs or php php-cs-fixer.phar fix --config-file=.php_cs

In addition
When run from dir with .php_cs file you can just run php php-cs-fixer.phar fix

You can also use this via Composer
Globally (Composer)
To install PHP-CS-Fixer, install Composer and issue the following command:

 $ ./composer.phar global require fabpot/php-cs-fixer

Then, make sure you have ~/.composer/vendor/bin in your PATH, and you're good to go:
export PATH="$PATH:$HOME/.composer/vendor/bin"

Then run from the project root
php-cs-fixer fix

@kauhat
Copy link

kauhat commented Apr 27, 2017

Just in case anyone else gets stuck with Class 'Symfony\CS\Config\Config' not found... errors like me - make sure that you haven't got version 2 of PHP-CS-Fixer installed.

Here's what I used to install v1:

$ composer global require friendsofphp/php-cs-fixer:1.*

@petericebear
Copy link
Author

Thank you for your addition! Sorry, just saw the contribution.

@BobbyBorisov
Copy link

Can anybody confirm to made it working with php-cs-fixer ver.2?

@jeystaats
Copy link

Yes , we changed it to this:

<?php
$finder = PhpCsFixer\Finder::create()
    ->notPath('bootstrap/cache')
    ->notPath('storage')
    ->notPath('vendor')
    ->in(__DIR__)
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true)
;

return PhpCsFixer\Config::create()
    ->setRules(array(
        '@Symfony' => true,
        'binary_operator_spaces' => ['align_double_arrow' => false],
        'array_syntax' => ['syntax' => 'short'],
        'linebreak_after_opening_tag' => true,
        'not_operator_with_successor_space' => true,
        'ordered_imports' => true,
        'phpdoc_order' => true,
    ))
    ->setFinder($finder)
;

@RomeroMsk
Copy link

Guys, thanks for sharing your configs!

Proposed changes for @jeystaats's config:

  • Change 'binary_operator_spaces' => ['align_double_arrow' => false] to 'binary_operator_spaces' => ['operators' => ['=>' => null]] (as align_double_arrow option is deprecated now).
  • [subjectively] Add 'phpdoc_no_empty_return' => false to comply with the Laravel base code (@return void is used everywhere).

Any ideas on how to add a rule to align phpdoc tags according to Laravel Coding Style (two spaces in @param tag)?

@autaut03
Copy link

autaut03 commented Oct 19, 2018

I also find these useful for myself:

'@Symfony:risky' => true,
'php_unit_construct' => false

@djallits
Copy link

Am I not mistaken, but doesn't the framework use 'ordered_imports' => ['sortAlgorithm' => 'length'],? Just curious here.

@coquer
Copy link

coquer commented Aug 18, 2019

laravel no longer sort the imports by length, but instead uses alphabetical order.

@petericebear
Copy link
Author

Oh men i really have to update this gist haha.
Will update it this week

@mreduar
Copy link

mreduar commented Nov 29, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment