Skip to content

Method factory on JWTGuard not found #2232

Closed
@kingkero

Description

Subject of the issue

Running static analysis such as PHPStan results in an error for using $guard->factory() even if $guard instanceof JWTGuard is true.

'expires_in' => auth()->factory()->getTTL() * 60

this doesn't pass PHPStan with the error message

Call to an undefined method                                                 
     Illuminate\Contracts\Auth\StatefulGuard&Tymon\JWTAuth\JWTGuard::factory().

Your environment

Q A
Bug? no / yes
New Feature? no
Framework Laravel
Framework version 10.29
Package version 2.0.0
PHP version 8.2

Steps to reproduce

In a Controller that uses the api:auth middleware, using

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Symfony\Component\Process\Exception\RuntimeException;
use Tymon\JWTAuth\JWTGuard;

class RefreshController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $guard = Auth('api');
        if (! $guard instanceof JWTGuard) {
            throw new RuntimeException('Wrong guard returned.');
        }

        return response()->json([
            'token_type' => 'Bearer',
            'access_token' => $guard->refresh(),
            'expires_in' => $guard->factory()->getTTL() * 60,
        ]);
    }
}

Expected behaviour

PHPStan should realize that ->factory() exists on the JWTGuard (via the jwt token)

Actual behaviour

Triggers the

Call to an undefined method
Illuminate\Contracts\Auth\StatefulGuard&Tymon\JWTAuth\JWTGuard::factory().

error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions