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

Question: Intellisense for Laravel helper methods #1601

Open
saade opened this issue Oct 22, 2024 · 3 comments
Open

Question: Intellisense for Laravel helper methods #1601

saade opened this issue Oct 22, 2024 · 3 comments
Labels

Comments

@saade
Copy link

saade commented Oct 22, 2024

Versions:

  • ide-helper Version: v3.1.0
  • Laravel Version: 11.28.1
  • PHP Version: 8.3.11

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

@saade saade added the question label Oct 22, 2024
@Tjaitil
Copy link

Tjaitil commented Oct 25, 2024

+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
Copy link

eldair commented Oct 29, 2024

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

@Tjaitil
Copy link

Tjaitil commented Nov 3, 2024

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants