bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.56k stars 92 forks source link

Undefined method 'createToken'. P1013 #2879

Closed yo-hersh closed 1 month ago

yo-hersh commented 1 month ago

Describe the bug I've encountered an issue with the php vscode-intelephense extension where it inaccurately reports an "Undefined method 'createToken()'" error for the createToken() method in Laravel's authentication system.

To Reproduce Set up a Laravel application with Laravel Sanctum for API token generation. Use the createToken() method to generate API tokens for authenticated users. Use the php vscode-intelephense extension for code editing and static analysis. The extension reports an "Undefined method 'createToken()'" error, even though the method is available in Laravel's authentication system.

Hare is the code snippet public function login(Request $request): JsonResponse { if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) { $user = Auth::user(); $success['token'] = $user->createToken('test')->plainTextToken; $success['name'] = $user->name;

        return $this->sendResponse($success, 'User login successfully.');
    } else {
        return $this->sendError('Unauthorised.', ['error' => 'Unauthorised']);
    }
}

Hare is the user model use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable { use HasFactory, Notifiable, HasApiTokens;

Expected behavior The php vscode-intelephense extension should correctly recognize the createToken() method in Laravel's authentication system and not report it as undefined.

Screenshots If applicable, add screenshots to help explain your problem.

Platform and version Intelephense version - [1.10.4 - 2024-03-26].

bmewburn commented 1 month ago

Auth::user() is probably returning an interface which does not have createToken declared on it. One solution is to "cast" the $user variable to the concrete type with an annotation.

/** @var \App\Models\User $user */
$user = Auth::user();

Another solution is to use https://github.com/barryvdh/laravel-ide-helper