Search form

Drupal 8 Module of the Week: Coder

Each day, more Drupal 7 modules are being migrated over to Drupal 8 and new ones are being created for the Drupal community’s latest major release. In this series, the Acquia Developer Center is profiling some of the most prominent, useful modules, projects, and tools available for Drupal 8. This week: Coder.

This week, we sat down with Klaus Purer (klausi on Drupal.org). He does backend development, Drupal architecture, and Devops at epiqo. He’s also a member of the Drupal Security Team and a Code Review Administrator on Drupal.org. Klaus worked on the Rules Module for Drupal 8 and maintains the coding standards implementation in the Coder project.

What does Coder do?

First and foremost, Coder is not a module anymore. It is a PHP command line tool that can be installed with Composer to automatically find and fix coding standard violations in your Drupal code. This is important because now you can easily integrate it into your automated testing process and flag errors when a changeset introduces coding standard errors (another example of this is in the Rules Project test configuration). There are also plans to include Coder into the automated patch testing on Drupal.org.

Coder uses the PHP_CodeSniffer Library and provides a set of so called "sniffs" that implement individual coding standard rules. It can be executed to detect coding standard problems using the phpcs command, but it can also automatically fix them using the phpcbf command.

Exact numbers are hard to come by--especially because it isn’t a Drupal module anymore--but Coder has been downloaded roughly 135,000 times. Coder previously shipped with a coder_upgrade module which helped when modules were upgraded from Drupal 6 to 7. That functionality is now covered by the Drupal Module Upgrader Project, which helps you rewrite your Drupal 7 module for Drupal 8.

Why is this important?

Klaus explains, “In order to better understand and read code faster, it is important that collaborating developers settle on a common set of standards for how they write code and what that code should look like. Drupal has its own coding standards and Coder is the implementation of those standards. When you’re writing code, Coder points out any style issues and can even fix most of them automatically for you so you don't have to guess if a piece of code is coding standards compliant. Coder can also integrate into your IDE or editor so that you get coding standard warnings while you write code.”

“Coder’s value really comes down to helping with developer productivity. Using Coder, developers are able to get up to speed faster when they join a project because it makes sure all code follows the Drupal coding standards--the standards they are used to. Coder helps to keep the code base in line with coding standards by reporting on style issues.”

When was Coder created?

Coder is almost 10 years old in 2016. It was originally developed as a Drupal module and has since been rewritten, based on PHPCS, and works independently of any specific Drupal installation. The problem it addresses was always the same: How can developers discover their coding standard mistakes automatically? How can they verify their code at this level for themselves?

“I got involved with Coder because I was reviewing project applications for Drupal.org and I was sick of manually pointing out coding standard errors to applicants,” explains Klaus. “That also resulted in http://pareview.sh/, created by Patrick Drotleff, that will run Coder on a public git repository for you. Coder as a service!”

Has Drupal 8 changed Coder?

Drupal 8 embraces many new PHP language features, such as namespaces, short array syntax, and object-oriented code. “Coder has implemented most of the new coding standard rules that have been defined for that, but we are still fine-tuning all the sniffs,” says Klaus. “Drupal 8 core even ships with a phpcs.xml.dist configuration file that indicates which coding standards Drupal core itself already fully passes. Yes, even Drupal core does not follow all coding standard rules. Coder is very picky.”

It is a smooth and easy process to add new coding standard rules or change and update existing ones. “It did take 2 years to develop Coder for Drupal 8, but there was always a working version at every stage along the way. We just had to refine or update the ruleset. It wasn’t hard since many rules could be re-used from the PHPCS mother project. I implemented that stuff in my free time instead of doing Sudoku puzzles during my commute on the train, for example. And I had help from many contributors that reported bugs and posted patches, thank you all!”

Each day, more Drupal 7 modules are being migrated over to Drupal 8 and new ones are being created for the Drupal community’s latest major release. In this series, the Acquia Developer Center is profiling some of the most prominent, useful modules, projects, and tools available for Drupal 8. This week: Coder.