PHP-CS-Fixer
It’s important that our development team follow the same standards when it comes to writing in PHP. These steps will give our PHP a baseline set of standards we can build on to keep us more efficient, maintain readability, and reduce whitespace code modifications during review.
- Install 
php-cs-fixer 
brew install php-cs-fixer
- Add the 
junstyle.php-cs-fixerextension to VS Code 
Within your VS Code settings.json file, add these lines…
// PHP CS FIXER
"php-cs-fixer.executablePath": "${extensionPath}/php-cs-fixer.phar",
"php-cs-fixer.executablePathWindows": "", //eg: php-cs-fixer.bat
"php-cs-fixer.onsave": true,
"php-cs-fixer.rules": "@Symfony",
"php-cs-fixer.config": "/Users/{your-user-directory}/Code/.php-cs-fixer.php;.php-cs-fixer.php.dist",
"php-cs-fixer.allowRisky": true,
"php-cs-fixer.pathMode": "override",
"php-cs-fixer.exclude": [],
"php-cs-fixer.autoFixByBracket": false,
"php-cs-fixer.autoFixBySemicolon": false,
"php-cs-fixer.formatHtml": false,
"php-cs-fixer.documentFormattingProvider": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[php]": {
    "editor.defaultFormatter": "junstyle.php-cs-fixer"
  },
"editor.formatOnSave": true
TIP
Make sure to change {your-user-directory} to your correct path.
If you need help editing your settings as a json.
- Create a new file called 
.php-cs-fixer.phpwithin yourCodedirectory. (If you don't have aCodedirectory, create one.) 
/Users/{your-user-directory}/Code/.php-cs-fixer.php
Set permissions to 755:
chmod 755 .php-cs-fixer.php
- Add this to the 
.php-cs-fixer.phpand save. 
<?php
$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
;
return (new PhpCsFixer\Config())
    ->setRules([
        '@Symfony' => true,
        'array_indentation' => true,
        'array_syntax' => ['syntax' => 'short'],
        'combine_consecutive_unsets' => true,
        'class_attributes_separation' => true,
        //'method_separation' => true,
        'multiline_whitespace_before_semicolons' => true,
        'single_quote' => true,
        'binary_operator_spaces' => [
            'default' => 'single_space',
            'operators' => [
                '=>' => 'align_single_space_minimal',
                '=' => 'align_single_space_minimal',
            ]
        ],
        'blank_line_before_statement' => true,
        'braces' => [
            'allow_single_line_closure' => true,
            'position_after_control_structures' => 'same',
            'position_after_functions_and_oop_constructs' => 'same'
        ],
        'cast_spaces' => ['space' => 'none'],
        // 'class_definition' => ['singleLine' => true],
        'concat_space' => ['spacing' => 'one'],
        'declare_equal_normalize' => true,
        'function_typehint_space' => true,
        //'hash_to_slash_comment' => true,
        'include' => true,
        'elseif' => true,
        'indentation_type' => true,
        'function_declaration' => true,
        'lowercase_cast' => true,
        'lowercase_keywords' => true,
        'method_chaining_indentation' => true,
        'native_function_casing' => true,
        'new_with_braces' => true,
        'yoda_style' => true,
        // 'no_blank_lines_after_class_opening' => true,
        // 'no_blank_lines_after_phpdoc' => true,
        // 'no_empty_comment' => true,
        // 'no_empty_phpdoc' => true,
        'no_empty_statement' => true,
        'no_extra_blank_lines' => [
            'tokens' => [
                'curly_brace_block',
                'extra',
                'parenthesis_brace_block',
                'square_brace_block',
                'throw',
                'use',
            ],
        ],
        // 'no_leading_import_slash' => true,
        // 'no_leading_namespace_whitespace' => true,
        'no_mixed_echo_print' => ['use' => 'print'],
        'no_multiline_whitespace_around_double_arrow' => true,
        'no_short_bool_cast' => true,
        'no_spaces_after_function_name' => true,
        'no_spaces_inside_parenthesis' => true,
        'no_useless_else' => true, 
        'no_singleline_whitespace_before_semicolons' => true,
        'no_spaces_around_offset' => true,
        'no_trailing_comma_in_list_call' => true,
        'no_trailing_comma_in_singleline_array' => true,
        // 'no_unneeded_control_parentheses' => true,
        'no_unused_imports' => true,
        'no_whitespace_before_comma_in_array' => true,
        'no_whitespace_in_blank_line' => true,
        // 'normalize_index_brace' => true,
        'object_operator_without_whitespace' => true,
        // 'php_unit_fqcn_annotation' => true,
        // 'phpdoc_align' => true,
        // 'phpdoc_annotation_without_dot' => true,
        // 'phpdoc_indent' => true,
        // 'phpdoc_inline_tag' => true,
        // 'phpdoc_no_access' => true,
        // 'phpdoc_no_alias_tag' => true,
        // 'phpdoc_no_empty_return' => true,
        // 'phpdoc_no_package' => true,
        // 'phpdoc_no_useless_inheritdoc' => true,
        // 'phpdoc_return_self_reference' => true,
        // 'phpdoc_scalar' => true,
        // 'phpdoc_separation' => true,
        // 'phpdoc_single_line_var_spacing' => true,
        // 'phpdoc_summary' => true,
        // 'phpdoc_to_comment' => true,
        // 'phpdoc_trim' => true,
        // 'phpdoc_types' => true,
        // 'phpdoc_var_without_name' => true,
        // 'pre_increment' => true,
        // 'return_type_declaration' => true,
        // 'self_accessor' => true,
        // 'short_scalar_cast' => true,
        'single_blank_line_before_namespace' => true,
        // 'single_class_element_per_statement' => true,
        // 'space_after_semicolon' => true,
        // 'standardize_not_equals' => 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,
        'semicolon_after_instruction' => false,
        'logical_operators' => true,
        'no_alternative_syntax' => false,
        'echo_tag_syntax' => ['format' => 'short'],
    ])
    ->setLineEnding("\n")
    ->setFinder($finder)
;
- Now upon saving a php file within VS Code, your code will be formatted according to this standard configuration.
 
If you aren’t seeing your code formatting after save you may need to reload the php cs fixer extension or even restart your VS Code application.
PHP Standards
As a baseline, we are following the Symfony coding standards. However this VS Code extension doesn’t completely cover all the standards listed. For instance, the extension will not update your variable names to be camel case instead of snake case, nor will it rename your files to UpperCamelCase.
There are also instances where we aren’t following the Symfony standards exactly. The most obvious difference is that we’ll put our opening braces on the same line as the function/class declaration, as opposed to the line beneath it. We’ll also keep assignment alignment for easier readability.
The VS Code extension will automatically align all the assignments, so there’s no need to add time doing this on your own if you don’t do it already.
Another important read: https://github.com/jupeter/clean-code-php
Note: if you are developing on an older project, we’ll want to disable the format on save features.
To do this, you’ll want to make sure there is a .vscode folder in the root of the project, and within that folder needs to be a settings.json file. The file needs to have these two lines …
{
  "php-cs-fixer.onsave": false,
  "editor.formatOnSave": false
}