logo
podcast Podcast
get help Get Unstuck

Should I type-hint the rules in a FormRequest?

Rethinking an example from a previous tip

Joel Clermont
Joel Clermont
2025-07-10

I previously shared a tip about using PHPStan type aliases to clean up some particularly ugly type signatures.

The specific example chosen was for the rules() return type in a FormRequest class.

On the one hand, this was a good example to use because there are so many different types of data you can pack into that rules() array.

But, I got a reply from a reader that raised a good point which made this example less than ideal: This method is only ever used by Laravel, it's never something I'd call in my code.

It's like a middleware or a controller action. All of these have complex type signatures, but they are never called directly in my code, so I just leave the types off.

The logic for form request rules is exactly the same. So I'm glad the reader called me out on this.

While it's technically possible I could stick something in that array that would cause a type error and prevent a bug, it's very unlikely. And my tests would definitely catch that. Type checking it is not really adding value in this case.

But the point in the previous tip still stands: type aliases are an easy way to clean up long type signatures in your code.

If you have places in your code where you don't like how long the type signature is getting, this is a great technique to use.

Here to help,

Joel

P.S. Want to reply to a tip and point out how I'm wrong? You can email, but it's even better to do it in real time in our Mastering Laravel community.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.