bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.61k stars 94 forks source link

[v1.10.3] Laravel app() return type being reported as null #2826

Closed eldair closed 3 months ago

eldair commented 6 months ago

Describe the bug Previous version did not report an error when calling Laravel app helper and this one does.

To Reproduce Calling app laravel helper

Expected behavior No error to be shown

Screenshots image

Platform and version VSCode running in wsl, 1.10.3 Intelephense version

bmewburn commented 6 months ago

I can't reproduce it. What version of laravel? If you hover over app() what signature does it show? Are there multiple signatures?

eldair commented 6 months ago

Hi @bmewburn , it's Laravel v11.0.8 and it happens in any file with that call. There is only a single signature image

frankiejarrett commented 6 months ago

@bmewburn Whoops I missed this and duplicated the same issue in https://github.com/bmewburn/vscode-intelephense/issues/2829

I am running Laravel v10.48.4. Rolling back to Intelephense 1.10.2 fixes the issue.

bmewburn commented 6 months ago

Do you have phpstorm metadata defined for app? Or any other ide helper definition for it? If so can you post it.

eldair commented 6 months ago

Nope :/ there is nothing for app in any meta file

bmewburn commented 6 months ago

Does a reindex fix it? ctrl + shift + p -> Index workspace

frankiejarrett commented 6 months ago

@bmewburn it does not, the only thing that fixed it for me was a rollback to 1.10.2 and a reindex

eldair commented 6 months ago

Same thing in v1.10.4 + reindex

eldair commented 6 months ago

@bmewburn I did some more tests and the issue disappears if I delete .phpstorm.meta.php generated by barryvdh/laravel-ide-helper (php artisan ide-helper:meta) even though there is no app override in the meta file

madebycaliper commented 6 months ago

Can confirm the same behavior and "solution" as @eldair . Removing .phpstorm.meta.php fixes the problem but this is obviously not a permanent solution. Running Laravel 10.47.0 with php 8.2.16. Thanks.

DannyJJK commented 4 months ago

I can confirm that fixes the issue as well. I don't know whether an easy solution is to prevent intelephense from scanning the .phpstorm.meta.php file? It's not actual project code, just something PHPStorm generates

tinusg commented 4 months ago

Anyone came up with a better solution? I'm also struggling with this.

00dani commented 4 months ago

I've done a little experimenting on Intelephense 1.10.4 and found a workaround for this issue. Adding an extra line to your .phpstorm.meta.php like so will allow Intelephense to correctly type the app()->make(MyClass::class) syntax, as well as any other calls to app() with no argument.

    override(\app(0), map([
        '' => '@',
        'null' => \Illuminate\Contracts\Foundation\Application::class,

I believe Intelephense is examining the function's signature, app(?string $abstract = null, array $parameters = []), and using that default null argument to infer the return type when no arguments are provided. Basically, it's looking up the type of app('null'), and the PHPStorm metadata says that any string argument is treated as a class-string and by default will return an object of that type ('' => '@'), unless it's overridden later on with a more specific type.

This workaround does mean that app('null') will be accepted and treated identically to app(), which isn't ideal, but you probably don't have a service named "null" and probably won't want to write app('null') in your code.

I believe the correct behaviour on Intelephense's part would be to ignore this override declaration completely unless the argument passed to app() really is a string, because as far as I can tell from JetBrains' documentation, the map() directive only works for string arguments anyway.