How to automatically apply the Laravel PHP code style
source link: https://barryvanveen.nl/articles/31-how-to-automatically-apply-the-laravel-php-code-style
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to automatically apply the Laravel PHP code style
Oct 04, 2016
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
Recommend
-
31
Freek Van der Herten is a developer and partner at Spatie.
-
45
README.md Laravel Auto Translate
-
10
Install Husky Husky is an excellent tool to automate hooks, in this post we are running phpunit tests before pushing code. npm install husky@4 --save-dev Enter fullscreen mo...
-
7
Apply meaningful metrics to makeover your product codeStefanie Kulberg06.30.21The software development industry is gearing up for the post-pandemic activ...
-
5
Laravel API to Postman This package allows you to automatically generate a Postman collection based on your API routes. It also provides basic configuration and support for bearer auth tokens for routes behind an auth middleware....
-
7
I'm proud to announce that our team has released a new package called spatie/laravel-route-discovery. This package can be used to automatically discover and register routes by looki...
-
8
Introduc...
-
3
VB Event Apply now to speak at the 2022 Low-Code / No-Code Summit 2022
-
4
-
4
Premature Memoization: How to Properly Apply It - Code Smell 250 Maximiliano Contieri
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK