Replies: 2 comments 4 replies
-
Middleware can't be used to narrow types because Larastan does not execute the request lifecycle. One thing you could do is extend the Request and override the class Request extends IlluminateRequest
{
public function user(): User
{
$user = parent::user();
abort_if($user === null, 500);
return $user;
}
// or
public function userNotNull(): User
{
$user = $this->user();
abort_if($user === null, 500);
return $user;
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
-
You could add a stubs file that "fixes" the return type. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I've managed to successfully increase the phpstan level of a large open source project to level 7 so far which has been a big help (thank you!), but I've been having some difficulty getting it to level 8 due to the inconvenience (read: ocd) of adding
abort_if($request->user() === null, 500);
throughout hundreds of routes. I will often either dependency inject form requests and use$request->user()
, or useauth()->user()
which both return a?User
type. But, even though I use the defaultIlluminate\Auth\Middleware\Authenticate
middleware to throw exceptions if it's ever null, from what I understand, the middleware isn't used to narrow the type down toUser
, causing 1000+ errors on level 8 compared to level 7.Is such a task possible with larastan or phpstan? I've never worked directly on phpstan or larastan before, but I would be open to spending a weekend or 2 on this depending on how difficult such a task might be. Is there a recommended place to start?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions