8000 Add user(): helper function by imliam · Pull Request #6582 · laravel/laravel · GitHub
[go: up one dir, main page]

Skip to content

Add user(): helper function #6582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Style fixes
  • Loading branch information
imliam committed Mar 15, 2025
commit a0a93a98d6419d43549aa41cae4d5fc4f99141ab
4 changes: 2 additions & 2 deletions bootstrap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function user(): User
throw new Exception('The current user is not authenticated.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, this could be a dedicated UnauthenticatedException or the like 🤔

}

if (!$currentUser instanceof User) {
throw new Exception('The currently authenticated user is not an instance of ' . User::class);
if (! $currentUser instanceof User) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the function has a return type of App\Models\User this check is unnecessary.

If an object which is not an instance of App\Models\User is returned, PHP will error out automatically.

Copy link
Author
@imliam imliam Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would throw a TypeError you're right, but I made it explicit here so that static analysis tools can tell what's going on

Another option would be to docblock the $currentUser variable for static analysis and allow the TypeError to be thrown at runtime

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the user model defined in the config: auth.providers.users.model.
It should use that.

I’m experiencing the same frustration with PHPStan. I’ll probably implement something like this in my projects, but I feel like a better version should be implemented at the framework level.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helpful idea, but I think using App\Models\User directly could be limiting. In many cases, the authenticated user isn't always a User instance — it can be any Authenticatable implementation, defined via auth.providers.users.model and possibly located in a different namespace.

Also, when calling auth()->user() (or this helper if added), developers typically expect an Authenticatable, not specifically a User model. This might lead to confusion or unexpected type errors in custom setups.

throw new Exception('The currently authenticated user is not an instance of '.User::class);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks in apps with multiple auth guards. It assumes only User gets authenticated.


return $currentUser;
Expand Down
0