Code Style
Tabs vs spaces
We use spaces at Built By Cactus.
Spaces allow us to set a maximum column width which makes diffs easier to read. For that reason alone spaces should be an easy choice.
Code style
When using PHP outside of WordPress, we should follow the PSR-1 and PSR-2 standards as closely as possible.
This makes our code easier to read and maintainable by other developers if needs be.
What does that mean?
In short, these are the main rules of PSR-1 and PSR-2.
PSR-1 is the basic coding standard for PHP and PSR-2 is the coding style guide. PSR-1 covers such requirements as:
- Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both
- Namespaces and classes MUST follow an “autoloading” PSR: [PSR-0, PSR-4]
- Class names MUST be declared in StudlyCaps
- Method names MUST be declared in camelCase
PSR-2 covers such requirements as:
- Code MUST use 4 spaces for indenting, not tabs
- There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or fewer.
- There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations.
- 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.
- Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.
Enforcing styles
There are ways we can enforce this within our editor, for example:
PhpStorm
- Open Preferences
- Go to Editor > Code Style
- Choose PHP
- Under Code Style choose Set From and select PSR1/PSR2
Editor config
If you don’t use PhpStorm, you may use a .editorconfig
file to set these standards. Most IDEs and editors now support
them. You can download a pre-set one from here
Linting
We can also lint our PHP to make sure it adheres to these guidelines. This can be done using some composer packages.1
Using composer we can call the PHP Codesniffer package. This can be installed as follows:
composer global require "squizlabs/php_codesniffer=*"
You can then use the command phpcs
from the command line to format your code.
First we need to check what standards are installed, we can do this as follows:
phpcs -i
# The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz and Zend
We can then run this over our code like this:
phpcs --standard=PSR1,PSR2 /path/to/code/
We should then see a list of errors that need to be fixed along with their files and lines.
Automatically Fixing Errors
To automatically fix style errors we can use the PHP-CS-Fixer package.
To install it, run to following from the command line:
composer global require friendsofphp/php-cs-fixer
You can then use the command below to fix any errors. By default, the rules will fix to the PSR-1 and PSR-2 standards, but they can be overridden as per the documentation
php-cs-fixer fix /path/to/code