vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

laravel-ide-helper broken with laravel 11

Open Nemesis19 opened this issue 1 year ago • 13 comments

Describe the bug Last versions of Intelephense are showing errors on model instances. See the screenshot. Screenshot 2024-07-24 alle 21 34 20

With error Expected type 'App\Models\Vehicle'. Found 'Illuminate\Database\Eloquent\TModel'.intelephense(P1006) @var \Illuminate\Database\Eloquent\TModel $vehicle

and for $section this is the error Expected type 'App\Models\Section'. Found 'Illuminate\Database\Eloquent\Builder|null'.intelephense(P1006) @var \Illuminate\Database\Eloquent\Builder|null $section

The only way to solve it is to put @var annotations like these (and not convenient to be added everywhere)

/** @var Vehicle|null $vehicle **/ /** @var Section|null $section **/ VehicleService::startWithSection($vehicle, $section);

it doesn't seem a problem many people have, so is there any configuration that it's causing this?

Thanks

Nemesis19 avatar Jul 24 '24 19:07 Nemesis19

It seems the template type TModel is not resolving to the actual type. To understand why, some more context is needed. What version of intelephense is this? What version of Laravel? Are you using https://github.com/barryvdh/laravel-ide-helper? If so, are you generating model docs?

bmewburn avatar Jul 24 '24 21:07 bmewburn

Latest Intelephense versionLaravel 11Yes I’m using that package and generating the model docs.Il giorno 24 lug 2024, alle ore 23:45, Ben Mewburn @.***> ha scritto: It seems the template type TModel is not resolving to the actual type. To understand why some more context is needed. What version of intelephense is this? What version of Laravel? Are you using https://github.com/barryvdh/laravel-ide-helper? If so, are you generating model docs?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

Nemesis19 avatar Jul 24 '24 22:07 Nemesis19

Latest intelephense version as in the pre-release (1.11) ? Or 1.10.4? When generating model docs is it to a separate file or the actual model file?

bmewburn avatar Jul 24 '24 23:07 bmewburn

Could be related to https://github.com/barryvdh/laravel-ide-helper/issues/1572, https://github.com/laravel/framework/pull/51851

bmewburn avatar Jul 25 '24 04:07 bmewburn

I'm sorry I didn't specify...latest stable v1.10.4. The models are generating inside the Model file. Thanks for pointing me to the right direction. I do agree the problems are probably related!

Nemesis19 avatar Jul 25 '24 07:07 Nemesis19

I don't know if it is related but I had a similar problem after upgrading some packages. Laravel was already updated to 11, so I think it works correctly with Laravel 11.

The problem is that the more you install vendor packages, the more you are likely to exceed the maximum file size.

To check if it you have the same problem, go to ViewOutput → select intelephense in the dropdown. You should see this error:

[Warn  - 16:10:12] file:///Users/myuser/projects/myproject/_ide_helper.php is over the maximum file size of 1000000 bytes.

I increased the limit in the settings and it now works as expected!

sebj54 avatar Jul 31 '24 14:07 sebj54

I don't know if it is related but I had a similar problem after upgrading some packages. Laravel was already updated to 11, so I think it works correctly with Laravel 11.

The problem is that the more you install vendor packages, the more you are likely to exceed the maximum file size.

To check if it you have the same problem, go to ViewOutput → select intelephense in the dropdown. You should see this error:

[Warn  - 16:10:12] file:///Users/myuser/projects/myproject/_ide_helper.php is over the maximum file size of 1000000 bytes.

I increased the limit in the settings and it now works as expected!

Non this is not my case unfortunately.

Nemesis19 avatar Sep 12 '24 15:09 Nemesis19

@bmewburn

It seems the template type TModel is not resolving to the actual type. To understand why, some more context is needed. What version of intelephense is this? What version of Laravel? Are you using https://github.com/barryvdh/laravel-ide-helper? If so, are you generating model docs?

I have the same issue.

Laravel version: 11.21 Inteliphense version: 1.12.6 Ide helper version: 3.1

IDE helper generates doc comments for Illuminate\Database\Eloquent\Model:

/**
 * 
 *
 * @mixin \Eloquent
 * @mixin \Illuminate\Database\Eloquent\Builder
 * @mixin \Illuminate\Database\Query\Builder
 */
abstract class Model ...

And Illuminate\Database\Eloquent\Builder has a template PHPDoc tag by default:

/**
 * @template TModel of \Illuminate\Database\Eloquent\Model
 * ...
 */
class Builder ...

The main problem is that the correct type isn't being passed down to the Builder class. I tried adding a generic PHPDoc to my model like this:

/**
 * @mixin \Illuminate\Database\Eloquent\Builder<\App\Models\MyModel>
 */
class MyModel ...

Unfortunately, this didn’t work with Intelephense. However, I believe this could be a possible solution because it works with another php intelisense.

tlevi101 avatar Sep 16 '24 18:09 tlevi101

@tlevi101 @mixin is available with the premium version.

bmewburn avatar Sep 16 '24 20:09 bmewburn

@tlevi101 @mixin is available with the premium version.

I do have premium, and everything else works with @mixin.

tlevi101 avatar Sep 17 '24 06:09 tlevi101

@tlevi101 \Illuminate\Database\Eloquent\Builder doesn't have static methods. The ide_helper generated Eloquent class declares the builder methods as static. I don't think it's the right solution to presume all mixin class functions are static.

I generated a helper file with laravel 11 and fixed it manually below (untested). Ideally the laravel-ide-helper maintainers will fix it. https://gist.github.com/bmewburn/0433032378015d3cfefae0baf872bcdd

Even though laravel 11 broke ide_helper it did improve the builder types in laravel itself. You can call MyModel::query() and get an instance of \Illuminate\Database\Eloquent\Builder<MyModel> which then gives access to all fluent builder methods while templated to the model class.

bmewburn avatar Sep 17 '24 09:09 bmewburn

لقد قمت بإنشاء ملف مساعد باستخدام laravel 11 وقمت بإصلاحه يدويًا أدناه (لم يتم اختباره). من الناحية المثالية، سيقوم المشرفون على laravel-ide-helper بإصلاحه. https://gist.github.com/bmewburn/0433032378015d3cfefae0baf872bcdd

engaboud avatar Sep 18 '24 01:09 engaboud

This issue has been fixed by barryvdh/laravel-ide-helper#1591. By updating Laravel IDE Helper to 3.2, you will be able to correctly extract model types.

KentarouTakeda avatar Oct 21 '24 07:10 KentarouTakeda

Thanks @KentarouTakeda ! That fixed part of the problem but I think the file created with php artisan ide-helper:generate is still broken as it doesn't handle templates or conditional return types.

bmewburn avatar Oct 21 '24 22:10 bmewburn

@bmewburn it will be fixed by https://github.com/barryvdh/laravel-ide-helper/pull/1631

chack1172 avatar Dec 20 '24 18:12 chack1172

composer update

composer require --dev barryvdh/laravel-ide-helper

php artisan ide-helper:generate php artisan ide-helper:models

Muhammed2024Salama avatar Dec 29 '24 12:12 Muhammed2024Salama