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.
PSR-2 must follow the "coding style guide", PSR-1, but also:
- Code MUST use 4 spaces for indenting and not use tabs
- There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less
- There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations
- Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body
- Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body
- Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility
- Control structure keywords MUST have one space after them; method and function calls MUST NOT
- Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body
- Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before
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": allowRisky: true executablePath: "/Users/[USERNAME]/.composer/vendor/bin/php-cs-fixer" executeOnSave: true fixerArguments: [ "--using-cache=no" "--no-interaction" ] 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.