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

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!

avatar

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"]
👍 1

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1188 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses