barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.21k stars 1.16k forks source link

Question: Intellisense for Laravel helper methods #1601

Open saade opened 2 weeks ago

saade commented 2 weeks ago

Versions:

Question:

When defining a macro, let's say for the Response class:

// app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Response;

Response::macro('api', function (?string $message = null, ?array $data = null, int $status = 200, array $headers = []) {
    return Response::json(
        data: ['message' => $message, 'data' => $data],
        status: $status,
        headers: $headers
    );
});

is it possible to get intellisense when using? :

return response()->api() // Undefined method 'api'

This also occurs when macroing Auth too:

use Illuminate\Support\Facades\Auth;

Auth::macro('admin', function (): ?Admin {
    /** @var Auth $this */
    return auth('admin')->user();
});
auth()->admin() // Undefined method 'admin'

PS: The method works as expected, just the intellisense cannot pick the defined macro PS2: Even auth()->user() which is a standard method is not being found

Tjaitil commented 1 week ago

+1. I was wondering about the exact same thing. I'm assuming one could use the 'interfaces' option in the ide-helper config to add docs to the interface. But I haven't been succesfull in making it work.

eldair commented 1 week ago

@Tjaitil same here, couldn't make interfaces option work

Tjaitil commented 3 days ago

My temporary solution is to make an custom ide-helper file with the macro docs and then include it in extra section in the ide-helper.config

<?php

namespace Illuminate\Contracts\Routing {
       interface ResponseFactory
       {
       /**
         * @param  array<int|string, mixed>  $data
         * @param  array<int, \App\ValueObjects\GameLog>|\App\ValueObjects\GameLog>  $messages
         * @param  int  $code
         * @param  array<int|string, mixed>  $headers
         * @param  int  $options
         * @return \Illuminate\Http\JsonResponse
         */
              public function jsonWithGameLogs($data = [], array|\App\ValueObjects\GameLog $messages = [], $code = 200, $headers = [], $options = 0);

       }
}
/** In ide-helper.config */
`    /*
    |--------------------------------------------------------------------------
    | Helper files to include
    |--------------------------------------------------------------------------
    |
    | Include helper files. By default not included, but can be toggled with the
    | -- helpers (-H) option. Extra helper files can be included.
    |
    */

    'include_helpers' => true,

    'helper_files' => [
        base_path().'/vendor/laravel/framework/src/Illuminate/Support/helpers.php',
        base_path().'_macro_ide_helper.php',
    ],

At least in my setup (intelephense and larastan) this extra doc file is providing everything that is needed.

Perhaps not the ideal solution, but works while we wait for another answer :)