Get better auto-complete from your factories

And reduce the need for @var docblocks

Joel Clermont
Joel Clermont
2024-02-12

I'm really enjoying the additional types being added to Laravel. For example, not too long ago, the factory classes added a docblock with a generic factory type.

It looks like this:

/**
 * @extends Factory<\App\Models\YourModelName>
 */

Why is this an improvement?

Without the generic type, any static analysis tool, including the auto-complete in your editor, wouldn't know that a Model::factory() call actually returns an object of the model's type.

As a result, if you wanted to auto-complete a relationship or property name, you'd need to add a @var docblock to tell your editor what type of object you were working with.

But with these new generic docblocks in factories, no @var docblock is needed, and auto-complete and other static analysis tools just work as expected.

If your project started before this change was made, it's worth going through your factories, adding the new generic docblocks, and then cleaning up any @var docblocks that are no longer needed.

Here to help,

Joel

P.S. Have you tried introducing Larastan into your project, but got stuck on a confusing error? I bet we could figure it out together.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.