laravel-ide-helper icon indicating copy to clipboard operation
laravel-ide-helper copied to clipboard

Easily replace or extend types of functions and methods of classes and interfaces

Open dmitrach opened this issue 4 years ago • 0 comments

Summary

I use VS Code. Now to get an expected type need to set the type manually, because the extension returns real types of methods (https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client).

// Case 1
/** @var User|null $authUser */
$authUser = auth()->user(); // \Illuminate\Contracts\Auth\Authenticatable|null from vendor/laravel/framework/src/Illuminate/Contracts/Auth/Guard.php
// Case 2
/** @var User $requestUser */
$requestUser = request()->user(); // mixed from vendor/laravel/framework/src/Illuminate/Http/Request.php
// Case 3
/** @var User $requestUser2 */
$requestUser2 = Request::user(); //  mixed from DocBlock of vendor/laravel/framework/src/Illuminate/Support/Facades/Request.php
/** @var User|null $authUser2 */
$authUser2 = \Auth::user(); // \Illuminate\Contracts\Auth\Authenticatable|null from DocBlock of vendor/laravel/framework/src/Illuminate/Support/Facades/Auth.php

For cases 3 and 4 I can extend facades and set @method static User user($guard = null) Returns the current user.. But the extended facades are empty and contains only replaced docblock.

It would be better when I easily change returning types for functions and classes without extends classes, like model hooks https://github.com/barryvdh/laravel-ide-helper#model-hooks

// For functions
$types = $command->getReturnTypes();
$types->addType(User::class);
$command->setReturnTypes($types);

// For classes
$method = $command->getMethod('user');
$method->addReturnType(User::class);
// or
$method->setReturnTypes(User::class, 'null');

dmitrach avatar May 19 '21 14:05 dmitrach