bmewburn/vscode-intelephense

laravel-ide-helper broken with laravel 11

Nemesis19 opened this issue · 15 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

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?

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?

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!

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!

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.

@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 @mixin is available with the premium version.

@tlevi101 @mixin is available with the premium version.

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

@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.

\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.

It works with this thank you.

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