Skip to main content
Tutorial Free

Laravel Pint Automated Code Formatting: Pre-commit Hooks and GitHub Actions

April 21, 2023
3 min read

Pint is a PHP code-style fixer that's specifically created to work with Laravel. In this tutorial, I will show you how it works and how to automate its launch before or after you commit code to the repository.


Installation

To install Pint, there's only one step - installing via Composer:

composer require laravel/pint --dev

And that's it! You're ready to use Pint.


Using Pint Locally with Git Hooks

One of the options to use Pint is pre-commit hooks. This is great for those, who want to push their code to the repository only when it's properly formatted. Here's what you need to do:

Create a file .git/hooks/pre-commit with the following content:

.git/hooks/pre-commit

#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM -- '*.php');
vendor/bin/pint $files -q
 
git add $files

Here's what this script does:

  • Gets a list of .php files that were changed
  • Runs Pint on those files
  • Adds those files to the commit

This allows seamless integration with Git. Once you attempt to commit something - it will reformat the code and push it to the repository.


Using Pint with GitHub Actions

Another option for Pint usage is GitHub Actions. For those unfamiliar, I have a separate video Advanced Laravel Testing: CI/CD with GitHub Actions.

It will run a check on GitHub automatically which will format all the files and make a new commit. To get this working, you need to do the following:

Create a file .github/workflows/pint.yml with the following content:

.github/workflows/pint.yml

name: PHP Linting (Pint)
on:
workflow_dispatch:
push:
branches-ignore:
- 'dependabot/npm_and_yarn/*'
jobs:
Pint:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.1'
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Launch Pint inspection
run: vendor/bin/pint
 
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: PHP Linting (Pint)
skip_fetch: true

Here's what we are doing here:

  • We are using the shivammathur/setup-php action to install the PHP
  • We are using the actions/checkout action to checkout the code
  • Copying .env file if it doesn't exist (just in case you want to do more than just Pint with this job)
  • Installing dependencies with Composer
  • Running Pint
  • Committing changes with commit message PHP Linting (Pint)

Once you commit this file, you should see that there's a new job in your Actions tab:

Clicking on it will give you all the information about the job:

And finally, you should see a new commit in your repository:

That's it! Now all your commits will be properly formatted after a push. Just don't forget to pull them!

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses on Laravel Daily

Laravel 13 Starter Kit Teams and Customizations

10 lessons
33 min

Roles and Permissions in Laravel 13

14 lessons
57 min

How to Structure Laravel 13 Projects

16 lessons
1 h 32 min read

Comments & Discussion

SP
Sylvain P ✓ Link copied!

for .gitlab-ci.yml

image: ubuntu:latest
 
stages:
- Pint
 
Pint:
stage: Pint
script:
- apt-get update && apt-get install -y php
- cp .env.example .env || true
- php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin --filename=composer
- composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- vendor/bin/pint
except:
- /^dependabot\/.*$/
tags:
- php
allow_failure: true
 
auto-commit:
stage: Pint
script:
- apt-get update && apt-get install -y git
- git config --global user.email "gitlab-ci@example.com"
- git config --global user.name "GitLab CI"
- git add --all
- git commit -m "PHP Linting (Pint)" || true
when: on_success
needs: ["Pint"]

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.