Vanilla PHP

Here are the Nymble guidelines for vanilla PHP development.

Tabs vs Spaces

Tabs or Spaces?

This is not a debate. 4 spaces are used for PHP development.

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.

There are ways we can enforce this within our editor, for example:


  • Open Preferences
  • Go to Editor > Code Style
  • Choose PHP
  • Under Code Style choose Set From and select PSR1/PSR2

Other Editors

Other editors may not have the built in formatting help that PhpStorm has, but we can still format our code correctly.

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 automatcially 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

Controller Structure

At Nymble we like to keep our code as lean and dry as possible. A big part of this is making sure we don’t overload our controllers with application logic.

Whilst some people like to move their logic from the controller to a model, we thing this is still wrong and promotes bad practices. Instead, we opt to use the Hexagonal Rails Architecture.

There’s a great video on it here.

The hexagonal architecture views the application as the hexagon in the middle. Every side of the hexagon represents some sort of a dialog the application needs to have with the outside world (e.g., talking to the database or interacting with the user).

It means that our application logic is separated from the code that uses it. This does two things:

  • Make our code easier to test in isolation.
  • Makes our code more reusable. E.G. Web interface, API, CLI can all use the same logic.

As such, we emplore all engineers to use this architecture when working with MVC.