Lint and Fix your Laravel Code with Duster and Husky

Ariel Mejia

July 19th, 2023 - 1 min read

As Tighten mentions on its blog

Duster is an opinionated linter and fixer for Laravel code, that takes the best of Laravel’s Pint, together with the power of PHP_CodeSniffer and PHP-CS-Fixer

Duster works on the command line, with Husky or use it's premade GitHub Action to run it in your CI pipeline

During installation & setup process I found a little issue related to Husky so here I share the solutions

Install Duster

composer require tightenco/duster --dev

Check that your project has git initialized

If not you can initialize it with

git init

Install Husky

npm i -D husky

Install Lint-Staged

npm i -D lint-staged

Initialize Husky

npx husky-init

Add Husky Pre-commit file

npx husky add ./.husky/pre-commit 'npx --no-install lint-staged'

Remove npm test command from pre-commit file

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no-install lint-staged
npm run test # this line should be removed

Configure Lint Staged for php files in package.json file

    ...
    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster lint"
        ]
    }
    ...

Instead of just lint your codebase, you can use the Duster fix command in order to fix the code base automatically.

Set Github Actions

Just execute

./vendor/bin/duster github-actions

It will publish a github action workflow for Duster, you can configure it and read more about Duster in the docs

Thanks for reading!


Ariel Mejia Illustration

Ariel Mejia

Laravel Senior Developer

Engineer with 7+ years of experience working in backend & frontend technologies
Open Source Maintainer of packages for Laravel community.

Stay up with Laravel

Sign up & get tips and tricks

You'll get monthly updates regarding my most recent articles and products.