Skip to content
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

Use config('auth.providers.user') for return type of Auth::user() and Request::user() #184

Closed
caugner opened this issue Jul 13, 2021 · 5 comments · Fixed by #326
Closed
Assignees
Labels
enhancement New feature or request

Comments

@caugner
Copy link
Contributor

caugner commented Jul 13, 2021

Is your feature request related to a problem? Please describe.

According to PHPDoc:

  • Illuminate\Support\Facades\Auth::user() returns \Illuminate\Contracts\Auth\Authenticatable|null
  • Illuminate\Http\Request::user() returns mixed

However, if providers.users.driver is set to eloquent in config/auth.php, they return the model configured in providers.users.model, which defaults to:

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

Describe the solution you'd like
The Psalm plugin should use that config to determine (and narrow) the return type of Auth::user() and $request->user().

Describe alternatives you've considered
(None.)

Additional context
(None.)

@caugner caugner added the enhancement New feature or request label Jul 13, 2021
@mr-feek
Copy link
Collaborator

mr-feek commented Jul 13, 2021

Huh, I thought ide-helper already provided this functionality!

@tm1000
Copy link
Contributor

tm1000 commented Jan 28, 2022

@mr-feek it does but the issue is that Psalm gets confused and also reads \Illuminate\Contracts\Auth\Authenticatable|null

tm1000 added a commit to tm1000/psalm-plugin-laravel that referenced this issue Jan 28, 2022
@mr-feek
Copy link
Collaborator

mr-feek commented Jun 17, 2022

I think I'm going to add in some dynamic analysis to stricten the return type of the config helper -- right now using the config helper can result in PossiblyInvalidCast.

We already do dynamic analysis for resolving container types -- it could also prove beneficial for resolving this issue

@tm1000
Copy link
Contributor

tm1000 commented Jun 24, 2022

@mr-feek sounds good to me I haven't gotten back to this because the tests are extremely complicated for this project. I only learned how they worked from the work on the 9.x release so I think I can actually get back to written a test here

@mr-feek
Copy link
Collaborator

mr-feek commented Jun 24, 2022

Feel free to take a look at #229 and https://github.com/nunomaduro/larastan/blob/master/src/Concerns/LoadsAuthModel.php for inspiration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants