← BACK
Laravel / Utilities

Automate Short Closure Conversions with Laravel Pint

Punyapal Shah • • 1 min read
edit this tip →

Enable the use_arrow_functions rule in pint.json to automatically refactor single-line closures into arrow functions.

Manually converting single-line function () use ($var) callbacks to fn () arrow functions across an entire project takes time.

Laravel Pint's use_arrow_functions rule automates this refactoring across your codebase.


Configuration

Enable the rule in your pint.json:

{
    "rules": {
        "use_arrow_functions": true
    }
}

What It Automatically Refactors

// Before Pint:
$active = array_filter($users, function ($user) use ($role) {
    return $user->hasRole($role);
});

// After Pint:
$active = array_filter($users, fn ($user) => $user->hasRole($role));

Key Benefits

  • No Manual use Bindings: Arrow functions automatically capture variables from the parent scope by value.
  • Consistent Syntax: Enforces modern PHP short closure syntax across all collection pipelines, array functions, and service providers.
  • Safe Transformation: Applies only to single-line return statements; complex multi-line closures remain untouched.

Related Tips

View all tips →
Laravel Utilities

Use Immutable Dates by Default in Laravel

Configure Laravel's Date facade to use CarbonImmutable by default, and automate migrating legacy Carbon calls with Rector.

READ >

// Got a tip in mind? Contributions are always welcome