Fix return type of `user` method inside form requests

0
open
MatanYadaev
MatanYadaev
Posted 1 month ago

Fix return type of `user` method inside form requests #1250

Summary

The return type of $this->user() inside a form request is mixed instead of User. When calling the same method from outside the form request the return type is correct $request->user() // User|null.

Code example

/**
 * @mixin IdeHelperUser
 */
class User extends Authenticatable
{
  //
}
class UserController
{
  public function index(GetUsersRequest $request)
  {
    $user = $request->user(); // User|null
    $user->id; // `id` is auto-completed
  }
}
class GetUsersRequest extends FormRequest
{
  public function authorize(): bool
  {
    $user = $this->user(); // mixed
    $user->id; // `id` isn't auto-completed
  }
}