PSR-2 Standards in PHP

As the team expands and changes over time, it is important that we follow standards so our code is readable by all members of the team. We have chosen the PSR-2 standard since this is the default code style for Laravel. Given the large number of Laravel projects which we now maintain, it makes sense to make this our default style.

The basics

PSR-2 must follow the "coding style guide", PSR-1, but also:

Using PHP CS Fixer within Atom

Fortunately, we can use PHP CS Fixer within an Atom package to apply all PSR-2 standards to our code as we write it. We can also use other rules within PHP CS Fixer to add further preferences to how we display our code.


First up, install PHP-CS-Fixer globally using composer:

composer global require friendsofphp/php-cs-fixer

Install package on Atom. Once installed, go to Atom -> Config and update php-cs-fixer with:

    allowRisky: true
    executablePath: "/Users/[USERNAME]/.composer/vendor/bin/php-cs-fixer"
    executeOnSave: true
    fixerArguments: [
    rules: "@PSR2,no_short_echo_tag,indentation_type,array_indentation,concat_space,function_typehint_space,linebreak_after_opening_tag,lowercase_static_reference,multiline_whitespace_before_semicolons,no_empty_comment,no_empty_statement,no_extra_blank_lines,no_leading_namespace_whitespace,no_mixed_echo_print,no_short_echo_tag,no_unused_imports,no_useless_else,no_useless_return,no_whitespace_in_blank_line,non_printable_character,normalize_index_brace,not_operator_with_space,object_operator_without_whitespace,ordered_imports,single_quote,ternary_operator_spaces,trim_array_spaces,trailing_comma_in_multiline_array,whitespace_after_comma_in_array,no_trailing_comma_in_singleline_array,no_multiline_whitespace_around_double_arrow,cast_spaces,declare_equal_normalize,blank_line_before_statement,binary_operator_spaces,no_singleline_whitespace_before_semicolons"
    showInfoNotifications: true

You will notice all the additional rules - these will format our PHP files to internal standards such as how we write arrays and handling whitespace. There is one 'risky' rule non_printable_character but in experience, invisible unicode characters have tended to cause issues.