{ Automate Backdrop Coding Standards on Your Projects }

Backdrop Coder


Backdrop coder is a packagist package that provides a library that phpcs uses to check your code against the standards defined on Backdrop CMS API site. To download and use the library is very easy and I'll show you how! Adhering to objective coding standards is a great way to keep your application lean and healthy. It is also an easy way to get started with Continuous Integration (CI) if you and your team have been eyeing that but unable to make the leap.

Install the Backdrop Coder Library


To install the backdrop/coder library we will use composer in my case I'm using Lando which installs composer for us on all PHP applications.


lando composer require backdrop/coder

The result of this command is that it downloads the backdrop/coder library and puts it into the vendor directory.

Now we have everything we need to run the code checking against the Backdorp codiing standards! Running this command will test the code in your custom modules:


lando php vendor/bin/phpcs --standard=vendor/backdrop/coder/coder_sniffer/Backdrop --extensions="module,inc,php" www/modules/custom/

Running the command will return you to command prompt if all is well with the code stanadards. If it finds errors it will output a report similar to this:



FILE: /app/www/modules/custom/serundeputy_tweets/serundeputy_tweets.module
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 7 | ERROR | Whitespace found at end of line
--------------------------------------------------------------------------------
UPGRADE TO PHP_CODESNIFFER 2.0 TO FIX ERRORS AUTOMATICALLY
--------------------------------------------------------------------------------


This error reports that on line 7 or the serundeputy_tweets.module there is whitespace at the end of a line. Perfect! Simple to read and to fix. Just open the file and remove that whitespace.

That is quite a lot to remember to type in order to run your code checking! So, lets wrap that up in a convenient composer script.

Configure composer.json and Running the Checks Locally


Here is a copy of my composer.json file:


{
    "require": {
        "abraham/twitteroauth": "^0.7.4",
        "backdrop/coder": "^1.0"
    },
    "scripts": {
        "test": [ 
            "vendor/bin/phpcs --standard=vendor/backdrop/coder/coder_sniffer/Backdrop --extensions=\"module,inc,php\" www/modules/custom/",
            "vendor/bin/phpcs --standard=vendor/backdrop/coder/coder_sniffer/Backdrop --extensions=\"module,inc,php\" www/themes/serundeputy"
        ] 
    }
}


The important bit for code standards is adding the scripts key with a test key and inside of test we drop two phpcs commands to test the code in www/modules/custom and www/themes/serundeputy, which will test the code in my custom modules and custom theme.

Now to run the whole shebang we just need a simple line:


lando composer test

Much easier!

Configure CI


Now to add it to our CI process (or make your first CI process if you don't already have one)! I'll use the Travis CI service. If you don't have a TravisCI account head over there and sign up for one and you can even sign in with your GitHub account. Once you are signed into Travis head over to your profile page and enable Travis for your code repo:

Screenshot of Travis CI config

Now add a .travis.yml config file to the root of your code repo to tell travis what tests to run:


# Configuration file for running the test suite. Results typically at http://travis-ci.org/backdrop/backdrop
# whitelist
language: php
php:
  - 7.0 
before_script:
  - composer install
script: composer test

So you see we are just installing the composer dependencies and then running the same testing command we did locally in CI: composer test.

Here is a failing test run from travis:

Screenshot of Travis CI run failng

And the corresponding run after fixing the errors:

Screenshot of Travis CI run passing

Now with each PR you and your team can make sure the code going into the project is up to spec and if you file a PR and it fails you can see that and fix it before you request code review by a human. Everyone is happier!

Conclusion


At first it can seem nit picky and annoying to get started with phpcs and code standards, but at the end of the day you'll end up with leaner, more efficient, self documenting applications. This in turn leads to happier developers, clients, and better productivity and profitability. It is also a low hanging fruit option to get jumpstarted with CI which up the level of your work again. So for a small amount of work and learning to get started you are forever upping your skill set and deliverables!

I say do it.

References and Tools:

  • Backdrop Coder ~ backdrop/coder package on packagist
  • Composer ~ PHP dependency manager
  • Lando ~ Local development environment isolating per app dev tools
  • phpcs ~ squizlabs/php_codesniffer code sniffer
  • Travis CI ~ Continuous Integration service

Follow me on twitter if you are so inclined: @serundeputy.