Skip to content

Instantly share code, notes, and snippets.

@meramsey
Last active September 30, 2021 13:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meramsey/3dc54492750e689c678bb4e9a11b0786 to your computer and use it in GitHub Desktop.
Save meramsey/3dc54492750e689c678bb4e9a11b0786 to your computer and use it in GitHub Desktop.
CS-Fixer setup
<?php
$finder = PhpCsFixer\Finder::create()
->exclude('libs')
->exclude('utils')
->name('*.phtml')
->in(__DIR__)
//->append([
//__DIR__.'/dev-tools/doc.php',
// __DIR__.'/php-cs-fixer', disabled, as we want to be able to run bootstrap file even on lower PHP version, to show nice message
//])
;
$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
// Some defaults in the new config should we keep?
//'@PHP71Migration:risky' => true,
//'@PHPUnit75Migration:risky' => true,
//'@PhpCsFixer' => true,
//'@PhpCsFixer:risky' => true,
//'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']],
// start custom_rules
'@PSR2' => true,
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:braces
'braces' => [
'position_after_functions_and_oop_constructs' => 'same',
'position_after_control_structures' => 'same',
'position_after_anonymous_constructs' => 'same',
],
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:function_declaration
// seems like this is already the default?
'function_declaration' => [
'closure_function_spacing' => 'one'
],
// overwrite some Symfony rules
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:concat_space
'concat_space' => [
'spacing' => 'one'
],
// convert to single spaces
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:single_quote
'single_quote' => true,
// @return void and @return null annotations should be omitted from PHPDoc.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:phpdoc_no_empty_return
'phpdoc_no_empty_return' => false,
// PHPDoc summary should end in either a full stop, exclamation mark, or question mark.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:phpdoc_summary
'phpdoc_summary' => false,
'trailing_comma_in_multiline' => false,
// additional rules
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:array_syntax
'array_syntax' => ['syntax' => 'short'],
// Remove useless (semicolon) statements.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:no_empty_statement
'no_empty_statement' => true,
// old syntax
// 'binary_operator_spaces' => [
// 'align_double_arrow' => null,
// 'align_equals' => null,
// ],
//@see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#changed-options
//@see https://stackoverflow.com/questions/58121806/possible-to-align-sequence-of-align-operators
//@see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/operator/binary_operator_spaces.rst
//@see https://gitter.im/FriendsOfPHP/PHP-CS-Fixer?at=5891c1fe1e4d4bd962bdc956
//@see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/config.rst
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:binary_operator_spaces
'binary_operator_spaces' => [
'operators' => [
'=' => null,
'=>' => null,
],
],
// Standardize spaces around ternary operator.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:ternary_operator_spaces
'ternary_operator_spaces' => false,
// Annotations in PHPDoc should be ordered so that @param annotations come first, then @throws annotations, then @return annotations.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:phpdoc_order
'phpdoc_order' => true,
// An empty line feed must precede any configured statement.
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:blank_line_before_statement
'blank_line_before_statement' => true,
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:no_extra_blank_lines
'no_extra_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block']],
// leave spaces inside parenthesis (override @PSR2)
'no_spaces_inside_parenthesis' => false,
//disable default which sets/adds this otherwise in php7.0+ "declare(strict_types = 1);" in some of the default rule sets
'declare_strict_types' => false,
// https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0|fixer:no_whitespace_in_blank_line
'no_whitespace_in_blank_line' => true
])
->setFinder($finder)->setUsingCache(false)->setIndent("\t");
return $config;

Context

Noticed when going to use cs-fixer locally the latest version showed our rules are deprecated...

PHP CS Fixer 2.19.0 Testament by Fabien Potencier and Dariusz Ruminski (d5b8a9d)
Runtime: PHP 7.4.3
Loaded config default from "/home/user/.php-cs-fixer.php".
Paths from configuration file have been overridden by paths provided as command arguments.
F
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) /home/user/PhpstormProjects/test.php (indentation_type, class_definition, braces, void_return, declare_strict_types, blank_line_after_opening_tag, no_unreachable_default_argument_value, psr_autoloading, blank_line_before_statement, header_comment)

Fixed all files in 0.017 seconds, 12.000 MB memory used

Detected deprecations in use:
- Given configuration is deprecated and will be removed in 3.0. Use configuration ['operators' => []] as replacement for ['align_double_arrow' => null, 'align_equals' => null].
- Rule "blank_line_before_return" is deprecated. Use "blank_line_before_statement" instead.
- Rule "no_extra_consecutive_blank_lines" is deprecated. Use "no_extra_blank_lines" instead.
- Rule "trailing_comma_in_multiline_array" is deprecated. Use "trailing_comma_in_multiline" instead.

Changes

Grabbed the brand new v3 configuration template from: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/.php_cs.dist

And spliced in rules in the new format and syntax as outlined in. https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/config.rst https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#changed-options

Considerations

The main thing that seems to be different is this one. Original

'binary_operator_spaces' => [
            'align_double_arrow' => null,
            'align_equals' => null,
        ],

New

'binary_operator_spaces' => [
            'operators' => [
                '='  => null,
                '=>' => null,
            ],
        ],

I believe I converted this but would really appreciate some confirmation cause it was kinda hard to lookup what it would do. i referenced the below links. https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/operator/binary_operator_spaces.rst https://stackoverflow.com/questions/58121806/possible-to-align-sequence-of-align-operators https://gitter.im/FriendsOfPHP/PHP-CS-Fixer?at=5891c1fe1e4d4bd962bdc956 ( last reply on this page scroll down)

I tested the new set with latest v3 release and v2(2.19.0) and it seems to work for me without issues in phpstorm and vscode. I was looking to use the same rules locally

For VScode you can use the same file with hardcoded path on local/remote or relative to the remote/workspace. image

/usr/local/src/cs-fixer/config/.php-cs-fixer.php,/home/user/.php-cs-fixer.php

Or the global one on both dev servers: image

Config file:

/usr/local/src/cs-fixer/config/.php-cs-fixer.php

Executable path(Optional as VS Code has its own copy in ${extensionPath}/php-cs-fixer.phar):

/usr/local/src/cs-fixer/php-cs-fixer
mkdir -p /usr/local/src/cs-fixer/config/ ;
wget -O /usr/local/src/cs-fixer/php-cs-fixer https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.0.0/php-cs-fixer.phar ;
chmod a+x /usr/local/src/cs-fixer/php-cs-fixer ;
wget -O /usr/local/src/cs-fixer/config/.php-cs-fixer.php https://gist.githubusercontent.com/meramsey/3dc54492750e689c678bb4e9a11b0786/raw/31b57e2c007f29cb9ac3ed6a96451d02e779858a/.php-cs-fixer.php ;
chmod a+x /usr/local/src/cs-fixer/config/.php-cs-fixer.php ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment