Announcing Duster, a Code Linter and Fixer for Laravel

Feature image: Announcing Duster, a Code Linter and Fixer for Laravel

At Tighten, we’ve long believed that individual programmers shouldn’t waste their time worrying about code formatting. Early in our history, Tighten alum Adam Wathan saw Matt wasting time reviewing code style issues and built an entire CI tool, Nitpick, in large part responding to that waste.

But as our opinions about how to write code—especially in Laravel—have grown, it’s been harder and harder to find the right tooling to enforce all of our code styles. Finally, we decided it was time to create our own tool for it. We’ve taken the best of all the other tools out there, thrown in a little of our own Laravel-specific magic, and it’s finally ready for release: introducing Duster.

Introducing Duster 1.0

Duster is an opinionated linter and fixer for Laravel code. It’s not a linting or fixing tool of its own, but rather a parent tool that takes the best of Laravel’s Pint, together with the power of PHP_CodeSniffer and PHP-CS-Fixer configured the Tighten way, sprinkles in a little bit of Tighten’s special Laravel-specific lints through Tlint, and packages it all up to create one easy-to-use, powerful, Tighten-flavored code style tool.

Because Duster is a linter and a fixer at the same time, you can use it to tell you when your code is out of sync with Duster's styles, or you can also use it to fix those issues. Duster works on the command line, but you can also integrate it with Husky to run it automatically in response to local Git triggers, or use our premade GitHub Action to run it in your CI pipeline.

What’s it do?

Duster installs and configures four tools for linting and fixing: Laravel Pint, PHP_Codesniffer, PHP-CS-Fixer, and Tighten’s Tlint. Duster configures each tool using Tighten’s presets. We’re using Pint as the basis of the styles, since that’s the Laravel preferred tool, and then using the other three tools to layer on more lints/fixes to cover even more ground.

Installation

You can install the package via Composer:

composer require tightenco/duster --dev

Usage

Duster can be run in one of two modes: lint or fix. Linting will report issues but won’t make any changes to your codebase, and fixing will report and fix issues found.

To lint everything:

./vendor/bin/duster lint

To fix everything:

./vendor/bin/duster fix

To run Duster only on files that have uncommitted changes according to Git, you can use the --dirty option:

./vendor/bin/duster lint --dirty
#or
./vendor/bin/duster fix --dirty

Configuration

Duster comes with Tighten’s preferred styles defined out of the box, but it’s also made to be configurable with the duster.json file.

You can add and remove tools, change the order in which tools run, and use local configurations for each tool to override Duster’s defaults. You can also customize the paths you’re running Duster on and even add other scripts—for example, you can add in a tool like PHPStan—as a part of your Duster stack.

Check out the customizations section of Duster’s documentation for examples.

GitHub Actions

If you want to run Duster as a GitHub Action, you can publish a GitHub Actions workflow:

./vendor/bin/duster github-actions

You have the option to fail the workflow if any issues are found, or auto-commit the fixes to the codebase, depending on what you prefer.

Husky

You can use Husky to automatically run Duster on your local changed files before every commit.

To install Husky and dependencies:

npx husky-init && npm install lint-staged --save-dev

Update Husky’s pre-commit.

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

Open the new pre-commit file and remove the npm test line.

Then configure lint-staged by updating your package.json.

Here we tell lint-staged to run duster for all *.php files:

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

You can also use Husky to lint/fix other file types using tools such as stylelint or prettier.

Conclusion

If you want a comprehensive code style linter and/or fixer for your Laravel applications, Duster is a robust, configurable tool that brings together the best existing tools out there. We hope you love it.

Get our latest insights in your inbox:

By submitting this form, you acknowledge our Privacy Notice.

Hey, let’s talk.
©2024 Tighten Co.
· Privacy Policy