Ensuring Laravel Code Styles

In 42mate we are very strict to follow the coding standards of the products that we use, so for example Drupal has a coding standard, Laravel uses the PSR standards, since we work in different kind of projects sometimes we get confused about the CS of the projects and we need something to help us to ensure the coding styles.

In Drupal projects we use the Coder module that will check the sources for style errors, however, for Laravel projects we use Code Sniffer, this is a lint tool for PHP projects.

To install Code Sniffer you can use composer and install it globally

 composer global require "squizlabs/php_codesniffer=*"

Once is installed, from the root of your laravel project you can run this command to check your styles

phpcs --extensions=php --standard=PSR2 app/

The output of this tool is something like this

vagrant@local:~/Code/api$ phpcs --extensions=php --standard=PSR2 app/

FILE: /home/vagrant/Code/api/app/Tracker.php
------------------------------------------------------------------------------------------------------------------------
FOUND 11 ERRORS AFFECTING 8 LINES
------------------------------------------------------------------------------------------------------------------------
 3 | ERROR | [x] There must be one blank line after the namespace declaration
 100 | ERROR | [x] Expected 1 space after IF keyword; 0 found
 100 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
 112 | ERROR | [x] Opening brace should be on a new line
 114 | ERROR | [x] Expected 1 space after IF keyword; 0 found
 114 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
 116 | ERROR | [x] Expected 1 space after closing brace; 0 found
 116 | ERROR | [x] Expected 1 space after ELSE keyword; 0 found
 120 | ERROR | [x] Function closing brace must go on the next line following the body; found 1 blank lines before brace
 131 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "true" but found "TRUE"
 132 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "null" but found "NULL"
------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------

Time: 2.12 secs; Memory: 10Mb

You must go line by line fixing your errors and running the tool again until you fix everything (no messages 🙂 )

You can hook this command on git to trigger this automatically and deny the commit if there is some code style errors.

You can also add this tool in your CI job to mark the integration with errors if there is style errors.

Also it is possible to fix this errors automatically with phpcbf, I’m not a fan of this automatic tools, however if you like it take a look to the help.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.