Larastan 3.16.0 came out on 4 December 2026, the same day as PHPStan 2.4.0. Larastan is the PHPStan extension that understands Laravel’s magic: facades, Eloquent relationships, macros and collections. This release makes it much smarter about macros and eager-loading callbacks, and it fixes a long list of small type mistakes that caused false errors in real apps.

There’s one important upgrade note in the release notes: Larastan 3.16.0 requires PHPStan 2.4.0 or newer within the 2.x series. PHP and Laravel requirements haven’t changed.

We tested the examples below on a fresh Laravel 13.45 app with Larastan 3.16.0 and PHPStan 2.4.0 at level 9. The output shown is what PHPStan actually reported.

Upgrade Larastan and PHPStan together

composer require --dev larastan/larastan:^3.16 phpstan/phpstan:^2.4 -W
vendor/bin/phpstan --version   # PHPStan - PHP Static Analysis Tool 2.4.0

The -W flag lets Composer update PHPStan at the same time. If you pin PHPStan to an older 2.x version in composer.json, loosen that constraint first, or Composer will refuse the upgrade.

A minimal phpstan.neon for a Laravel app looks like this:

includes:
    - vendor/larastan/larastan/extension.neon

parameters:
    level: 9
    paths:
        - app

Macros now get real return types

Before this release, a macro without a declared return type was a blind spot. Larastan 3.16 infers the macro’s return type from the closure body. When the macro calls methods on $this, it also works out the model or collection item type.

Here are two macros registered in a service provider. Neither closure declares a return type:

// app/Providers/MacroServiceProvider.php
namespace App\Providers;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class MacroServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Str::macro('initials', function (string $name) {
            return collect(explode(' ', $name))
                ->map(fn (string $part) => mb_substr($part, 0, 1))
                ->implode('');
        });

        Collection::macro('evens', function () {
            return $this->filter(fn ($value) => is_int($value) && $value % 2 === 0)->values();
        });
    }
}

Remember to register the provider in bootstrap/providers.php. Larastan boots your application to discover macros, so an unregistered provider means “undefined method” errors.

Now we call them and ask PHPStan to dump the types:

\PHPStan\dumpType(Str::initials('Taylor Otwell'));
\PHPStan\dumpType(collect([1, 2, 3, 4])->evens());

PHPStan reported:

Dumped type: string
Dumped type: Illuminate\Support\Collection<int, int>

The second line is the interesting one. Larastan understood that $this inside the macro is the collection it was called on, so it kept the int item type. Code that chains more calls after evens() now gets checked properly.

Larastan 3.16 also resolves macros on classes that define their own __call(), including your application classes. It checks relation macros before forwarding calls to the Eloquent builder.

Wrong eager-loading callbacks are now caught

When you pass constraint closures to with(), withOnly(), load() or loadMissing(), Larastan now infers each callback’s relation type from its array key. A callback typed for the wrong relation is reported.

Given a User model with a posts() relationship that returns HasMany<Post, $this>:

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

// Correct: "posts" is a HasMany
User::with(['posts' => fn (HasMany $query) => $query->latest()])->get();

// Wrong: "posts" is not a BelongsTo
User::with(['posts' => fn (BelongsTo $query) => $query->latest()])->get();

PHPStan flagged only the second call, with the identifier argument.type. Its tip explained that offset 'posts' expects a closure taking HasMany<App\Models\Post, App\Models\User>. This catches a common copy-paste bug where a callback written for one relation ends up on another.

Because the callback parameter type is now known, you can also drop the explicit type hint. In our test, an untyped function ($query) { ... } callback for posts was dumped as HasMany<App\Models\Post, App\Models\User>, so PHPStan checks the calls you make inside it.

Smaller fixes you’ll notice

The release fixes several types that were too wide or wrong:

  • Collection::offsetGet() returns the collection’s value type without adding null. In our test, $names['first'] on a Collection<string, string> dumped as string.
  • base_path() returns non-empty-string when you pass a non-empty path.
  • Eloquent’s getArrayableAttributes() is typed as array<string, mixed>.
  • Dynamic where*() calls on the query builder, Fluent setters and dynamic with*() calls on mailables, views and redirect responses keep the receiver type, so chains don’t lose their type halfway.
  • Optional forwards method calls based on the wrapped value, and TestComponent forwards methods and properties from the Blade component under test.

Migration-based model properties

If Larastan reads your model properties from migrations, two fixes matter:

  • nullable() is respected on timestampTz(), timeTz() and year() columns, and softDeletesDatetime() columns are read as nullable.
  • year() columns are read as integers, and the nullable created_at and updated_at columns created by datetimes() are recognised.

These fixes can change your results in both directions. Errors that were false positives disappear, but code that ignored a nullable column may now get a new, correct error.

A safe upgrade routine

  1. Upgrade both packages in a branch with the Composer command above.
  2. Run vendor/bin/phpstan analyse and compare with your previous run.
  3. If you use a baseline, regenerate it only after you’ve looked at the new errors: vendor/bin/phpstan analyse --generate-baseline. New errors about eager-loading callbacks or nullable columns are usually real bugs.
  4. Delete baseline entries that now show as “unmatched”. PHPStan reports these when an ignored error no longer happens.

PHPStan 2.4.0 itself brings big new features too, such as recursive and generic type aliases, which Larastan users get automatically.

Takeaway

Larastan 3.16 closes two gaps that Laravel developers hit every day: macros with unknown return types, and eager-loading closures that nobody checked. Upgrade it together with PHPStan 2.4, register any macro providers, and review new errors before you refresh your baseline. The full list of changes is in the Larastan 3.16.0 release notes.