Slevomat Coding Standard

Ondřej Mirtes
Dec 20, 2015 · 2 min read
Image for post
Image for post

Every developer team should have a good infrastructure providing and enforcing consistent output of each team member at their fingertips. Coding standard is one of many things holding a project together.

It should not be obvious from a code snippet who wrote it. The entire team should have common conventions how to write code. Some can be checked automatically (code formatting), others (like sticking to designed architecture of the system) must be discussed during code review.

At Slevomat, we’ve had a very strict coding standard for one and a half years called Consistence Coding Standard. It has been checked and enforced in every pull request by PHP_CodeSniffer and Jenkins. Six months ago, we added more than a handful of advanced sniffs that we commited temporarily into our private repository. Some even support automatic fixing of violatons, making integration into projects much easier.

I’ve been waiting for this day for a long time. We are open-sourcing these sniffs so everybody can benefit from them too. I would like to highlight the ones I consider most interesting and most useful:

Unused private properties and methods

SlevomatCodingStandard.Classes.UnusedPrivateElements

Although PHP_CodeSniffer is not suitable for static analysis because it is limited to analysing one file at a time, it is possible to use it to perform certain checks. This sniff checks for unused methods and unused or write-only properties in a class. Reported unused elements are safe to remove.

This is very useful during refactoring to clean up dead code and injected dependencies.

Trailing array comma

SlevomatCodingStandard.Arrays.TrailingArrayComma

Commas after last element in an array make adding a new element easier and result in a cleaner versioning diff.

Yoda conditions

SlevomatCodingStandard.ControlStructures.YodaComparison

Yoda conditions decrease code comprehensibility and readability by switching operands around comparison operators forcing the reader to read the code in an unnatural way. This sniff finds and even automatically fixes such expressions.

Alphabetically sorted uses

SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses

Great coding standard should enforce unified code style in every place that can be evaluated objectively. This sniff prevents fighting over what is the correct order of imports at the top of each file.

Unused uses

SlevomatCodingStandard.Namespaces.UnusedUses

Another kind of dead code detection. This sniff can find and even delete extra uses. It can work with references in annotations (typically used by Doctrine Annotations) too.

Start using it today!

Above-mentioned sniffs are not the only ones we released today. You can find the complete list, comprehensive manual how to start using the whole standard (or just a handful of sniffs you find useful, no pressure!) and the source code on GitHub. We believe the code is so thoroughly tested and stable that we didn’t hesitate to release version 1.0.0.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store