Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

Lint and Fix Your Laravel Code with Duster

Published on by

Lint and Fix Your Laravel Code with Duster image

Today Tighten announced the 1.0 release of Laravel-focused code linter and fixer Duster.

Duster is a tool that brings together Laravel Pint, PHP_CodeSniffer, PHP-CS-Fixer, and Tighten's Laravel-specific lints in Tlint to provide a powerful and comprehensive linting and fixing toolset for Laravel apps.

If you've read our articles about sharing PHPCS rules or sharing PHP-CS-Fixer rules, you're already familiar with the idea of publishing a set of your own rules for your projects. Duster takes Laravel's base opinions in Pint and then adds the power of additional linters and fixers through the other three tools it bundles. By default it sticks to Tighten's code style, but it's also completely configurable to your preferences.

Let's take a look at how to install Duster, how to run it, how to integrate it into your automated workflows, and how to to configure it (if you want--you may be happy with its default rules!)

Installing Duster

There are a few ways you can run Duster on your application, but the simplest way to start is to install it as a Composer dependency in your app.

composer require tightenco/duster --dev

You don't need to publish or configure anything; Duster comes with an opinionated set of styles out of the box, and if you like them, it's ready to go as soon it finishes installing.

Running Duster

There are two main features Duster provides: "linting" and "fixing". Lints tell you when something in your code is out of sync with a rule; fixing fixes that code.

First, we can do the basics: Linting or fixing the entire codebase.

If we run lint, it'll give us a lint of the entire codebase, running all the linting tools:

./vendor/bin/duster lint

This will give us the output for each tool, and, like any linter, will return with a success or failure code that can be used in CI tools or other scripts.

We can also run fix to tell all the included tools to fix any issues they can in the entire codebase:

./vendor/bin/duster fix

Only linting "dirty" files

When introducing a linter/fixer to an existing codebase, it can often seem overwhelming: there are so many little fixes you need to make you may be tempted to throw the entire thing away.

One way you can avoid running any fixes (or getting a ton of lint failures) for code that you're not working on at the moment is Duster's --dirty flag, which only runs the linters/fixers on files that have un-committed changes.

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

Why fixing and linting?

There are two main reasons why Duster, and many tools like it, offer both linting and fixing. Why add linting when you can always just fix everything?

First, some teams may prefer a workflow where incorrect code is surfaced as a failing build (e.g. as a GitHub Action) instead of fixed.

And second, some lints can't be fixed automatically. A computer can tell if your code is broken but isn't smart enough to fix it for you.

Integrating Duster Into Your CI

Like most code analysis tools, Duster's lint command returns a success or error code based on whether the lints succeeded. This means you can use ./vendor/bin/duster lint in any CI pipeline to fail your builds if your lints aren't matched. You can also use ./vendor/bin/duster fix as a part of a Husky hook or a CI hook to automatically format your code.

If you use GitHub Actions, Duster makes it easy to publish an action configuration that will either lint your code or fix it. Run ./vendor/bin/duster github-actions and follow the prompts there to add the GitHub Action to your codebase.

Configuring Duster (and its tools)

Like Pint, Duster embodies the opinions of its creators (Tighten) about how code should be styled. But Duster itself, and each of the tools it imports, can be configured to your liking.

duster.json

Duster provides its own configuration file, duster.json. This file allows you to define files and folders to include or exclude from the Laravel-default files it targets by default. You can also use it to define additional scripts you'd like to run as a part of your duster flow.

For example, you can add PHPStan to your lint command with the following duster.json:

{
"scripts": {
"lint": {
"phpstan": ["./vendor/bin/phpstan", "analyse"]
}
}
}

You can also define your own custom additions to the fix command.

Duster's dependencies

You can configure each of Duster's dependencies with their native configuration files; you can learn more about how in the Customizing section of the Duster readme.

  • Pint: pint.json
  • PHP_CodeSniffer: .phpcs.xml.dist
  • PHP-CS-Fixer: .php-cs-fixer.dist.php
  • Tlint: tlint.json

In Summary

That's it! In summary, Duster is a tool for linting and fixing code style issues in your Laravel apps. It's opinionated, holding to Tighten's preferred standards out of the box, but it's also configurable to whatever you like--including your own custom standards, if you've published them.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
The Inertia.js v2 Beta is Here image

The Inertia.js v2 Beta is Here

Read article
Mastering Laravel, No Compromises, and SourceDive with Joel Clermont image

Mastering Laravel, No Compromises, and SourceDive with Joel Clermont

Read article
Now you can install PHP and the Laravel installer with a single command image

Now you can install PHP and the Laravel installer with a single command

Read article
WireSpy is a Sleek New Debug Bar for Laravel Livewire image

WireSpy is a Sleek New Debug Bar for Laravel Livewire

Read article
The Laracon AU schedule is now live image

The Laracon AU schedule is now live

Read article
Supercharge PhpStorm with Laravel Idea image

Supercharge PhpStorm with Laravel Idea

Read article