How to automatically apply the Laravel PHP code style

Writing clean and readable code is essential. Having a properly defined code style, and adhering to that, is tedious work. Let's automate it as much as possible.

Laravel code style

Laravel uses the PSR-1 and PSR-2 code style standards. These conventions define things like:

  • Class names MUST be declared in StudlyCaps.
  • Method names MUST be declared in camelCase.
  • Code MUST use 4 spaces for indenting, not tabs.

These are the obvious ones, some rules are more detailed and less easy to remember and stick to. It costs a lot of energy to memorize all rules and be strict about using them.

That is why automatic code style checkers are so great, they can save you a lot of work!

Editor

Many editors allow you to configure your preferred code style. My experience is that it is hard to configure and get exactly right. Also, I haven't found a predefined set of rules that works for me. That's why the following tools are so useful.

PHP-CS-Fixer

I've been happily using friendsofphp/php-cs-fixer for quite some time now. You can install it using Composer and it has a .php_cs config file that you can commit to your repository.

My .php_cs config is copied from laravel/framework but I've excluded checking on some directories and files. It still uses the same fixers so the code style is exactly the same as the Laravel core.

Simply run php-cs-fixer fix to check and fix all issues in your repository!

StyleCI

Laravel Framework uses StyleCI to automatically check for code style issues on new commits and pull requests.

StyleCI allows you to deal with issues in multiple ways:

  • It can notify you when it finds issues.
  • It can automatically send (and merge) fixes through pull requests.
  • It can automatically commit fixes.

Setup is easy with the .styleci.yml config file and they have Laravel code style preset. It's also free for open source projects!

How do you follow code style standards? Do you use different tools? What are your experiences? Feel free to leave a comment with your thoughts!

More articles

Comments (4)

Got a question? Liked the article or got a suggestion? Leave a comment to let us know.

Thank you for the tips. :)
Gonna try CS fixer, thanks for the tip!
Laravel 5.7 excluded .php_cs file from their repo. I wonder why? Probably the best solution is style ci?
Interesting observation Bobby! I could not find when this change has happened but it would be nice to know the reasoning behind it.

Personally, I like PHP-CS-Fixer (or PHP CodeSniffer) better than StyleCI as it prevents any code style violations from being committed. You can hook it up through pre-commit hooks or run it manually. With StyleCI you kind of repair the problem after it has been committed and I don't like separate commits for code style changes.