Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wp-mail-logging domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /mnt/serverpilot1_srv/shawnhooper/apps/shawn-hooper/public/wp-includes/functions.php on line 6114
Style PHP The Laravel Way with Laravel Pint - Shawn Hooper Skip to content

Style PHP The Laravel Way with Laravel Pint

I upgraded my CI Pipeline to use Laravel Pint today, and I love it!

Keeping your application’s code styled consistently is important, especially when the project has multiple developers. It makes the code easier to read, which you’ll be thankful for, either when doing a code review, or having to go back and update your own code later. I’ve used PHP-CS-Fixer to do this for many years.

The team at Laravel have released a new product called Laravel Pint, which is an opinionated code style fixer.

Under the hood, it’s still PHP-CS-Fixer, but just pre-configured to style things “The Laravel Way”. It can also style for PSR12 or Symfony standards, if you’d prefer.

Installing Pint

To add Pint to your existing project, install the composer package as a development dependency:

composer require laravel/pint --dev

or, if you’re using Laravel Sail:

sail composer require laravel/pint --dev

Running Pint on your local machine:

To run Pint, and update your PHP code style, run:

./vendor/bin/pint

or, if you’re using Laravel Sail, run this instead:

sail bin pint

Pint will scan each of your files for potential style updates, and produce output like this when complete:

Screenshot of Laravel Pint's output, reporting 1 style issue fixed to my User model file.
Laravel Pint ouput, showing 1 style issue fixed.

In this case, it shows that 1 style issue was fixed in my User model.

Automating Laravel Pint in your CI with GitHub Actions

Here’s the GitHub Actions workflow I’ve implemented to run Laravel Pint each time code in a pull request is updated. It:

  • Checks out the code from the repository
  • Runs Laravel Pint, fixing all code styling issues
  • Commits any changes to the repo using the Git Auto Commit action.

In order to set this up, create a .github/workflows/laravel-pint.yml file in your repo:

on:
pull_request:
name: Static Code Analysis
jobs:
phplint:
name: "Laravel Pint (Style)"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: "Laravel Pint"
uses: aglipanci/laravel-pint-action@0.1.0
with:
preset: laravel
- name: Commit changes to GitHub
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "PHP Style Change (Laravel Pint in CI)"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.