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.


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:


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:


name: PHP Linting (Pint)
- 'dependabot/npm_and_yarn/*'
runs-on: ubuntu-latest
contents: write
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
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
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!


for .gitlab-ci.yml

image: ubuntu:latest

  - Pint

  stage: Pint
    - apt-get update && apt-get install -y php
    - cp .env.example .env || true
    - php -r "readfile('');" | php -- --install-dir=/usr/local/bin --filename=composer
    - composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - vendor/bin/pint
    - /^dependabot\/.*$/
    - php
  allow_failure: true

  stage: Pint
    - apt-get update && apt-get install -y git
    - git config --global ""
    - git config --global "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:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 79 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials