Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

PhpLaravelVisual Studio-Code

Php Problem Overview


After the latest update of PHP Intelephense that I get today, the intelephense keep showing an error for an undefined symbol for my route (and other class too), there is no error like this before and it's bothering me.

Here is the error screenshot :

enter image description here

And this is my code :

Route::group(['prefix' => 'user', 'namespace' => 'Membership', 'name' => 'user.'], function () {
    Route::get('profile', 'ProfileController@show')->name('profile.show');
    Route::patch('profile', 'ProfileController@update')->name('profile.update');
    Route::patch('change-password', 'ChangePasswordController@change')->name('change-password');
    Route::get('role', 'ProfileController@getRole')->name('profile.role');
    Route::get('summary', 'SummaryController@show')->name('summary');
    Route::get('reserved', 'AuctionController@reservedAuction')->name('reserved');
});

Actually there's no error in this code but the intelephense keeps showing an error so is there a way to fix this?

Php Solutions


Solution 1 - Php

Intelephense 1.3 added undefined type, function, constant, class constant, method, and property diagnostics, where previously in 1.2 there was only undefined variable diagnostics.

Some frameworks are written in a way that provide convenient shortcuts for the user but make it difficult for static analysis engines to discover symbols that are available at runtime.

Stub generators like https://github.com/barryvdh/laravel-ide-helper help fill the gap here and using this with Laravel will take care of many of the false diagnostics by providing concrete definitions of symbols that can be easily discovered.

Still, PHP is a very flexible language and there may be other instances of false undefined symbols depending on how code is written. For this reason, since 1.3.3, intelephense has config options to enable/disable each category of undefined symbol to suit the workspace and coding style.

These options are: intelephense.diagnostics.undefinedTypes intelephense.diagnostics.undefinedFunctions intelephense.diagnostics.undefinedConstants intelephense.diagnostics.undefinedClassConstants intelephense.diagnostics.undefinedMethods intelephense.diagnostics.undefinedProperties intelephense.diagnostics.undefinedVariables

Setting all of these to false except intelephense.diagnostics.undefinedVariables will give version 1.2 behaviour. See the VSCode settings UI and search for intelephense.

Solution 2 - Php

Version 1.3.0 has flaw IMO.
Downgrade to version 1.2.3 fixes my problem.

I'm on

  • Laravel 5.1
  • PHP 5.6.40
Downgrade to Version 1.2.3

Solution 3 - Php

use Illuminate\Support\Facades\Route;

Warning Disappeared after importing the corresponding namespace.

Version's

  • Larvel 6+
  • vscode version 1.40.2
  • php intelephense 1.3.1

Solution 4 - Php

This solution may help you if you know your problem is limited to Facades and you are running Laravel 5.5 or above.

Install laravel-ide-helper

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

Add this conditional statement in your AppServiceProvider to register the helper class.

public function register()
{
    if ($this->app->environment() !== 'production') {
        $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
    }
    // ...
}

Then run php artisan ide-helper:generate to generate a file to help the IDE understand Facades. You will need to restart Visual Studio Code.

References

https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/16

https://github.com/barryvdh/laravel-ide-helper

Solution 5 - Php

In my case, for some reason, vendor folder was disabled on VS Code settings:

    "intelephense.files.exclude": [
        "**/.git/**",
        "**/.svn/**",
        "**/.hg/**",
        "**/CVS/**",
        "**/.DS_Store/**",
        "**/node_modules/**",
        "**/bower_components/**",
        "**/vendor/**", <-- remove this line!
        "**/resources/views/**"
    ],

By removing the line containing vendor folder it works ok on version Intelephense 1.5.4

Solution 6 - Php

You don't need to downgrade you can:

Either disable undefined symbol diagnostics in the settings -- "intelephense.diagnostics.undefinedSymbols": false .

Or use an ide helper that adds stubs for laravel facades. See https://github.com/barryvdh/laravel-ide-helper

Solution 7 - Php

If you see this immediately after adding a new Vendor class, be sure to run the VScode command (control-shift-P) Index Workspace

Solution 8 - Php

1.3.1 fixed it.

Just update your extension and you should be good to go

Solution 9 - Php

To those would prefer to keep it simple, stupid; If you rather get rid of the notices instead of installing a helper or downgrading, simply disable the error in your settings.json by adding this:

"intelephense.diagnostics.undefinedTypes": false

Solution 10 - Php

Here is I solved:

Open the extension settings:

enter image description here

And search for the variable you want to change, and unchecked/checked it

enter image description here

The variables you should consider are:

intelephense.diagnostics.undefinedTypes 
intelephense.diagnostics.undefinedFunctions         
intelephense.diagnostics.undefinedConstants         
intelephense.diagnostics.undefinedClassConstants 
intelephense.diagnostics.undefinedMethods 
intelephense.diagnostics.undefinedProperties 
intelephense.diagnostics.undefinedVariables

Solution 11 - Php

This is really a set of configurations for your editor to understand Laravel.

If you want to configure it all manually, here is the repo. This is for both VS code and PhpStorm.

Or if you want you can download this package.(I created) recommended to install it globally.

And then just run andylaravel setupIDE. this will configure everything for you according to the fist repo.

Solution 12 - Php

There is an other solution since version 1.7.1 (2021-05-02)

You can now tell where intelephense should look for a dependency, for example vendor which is the most common.

"intelephense.environment.includePaths": [
    "vendor"
],

Furthermore, it even bypass the VSCode rule

"files.exclude": {
    "**/vendor": true
},

You can read more in the changelog here

Solution 13 - Php

No, the errors occurs only after the Intelephense extension is automatically updated.

To solve the problem, you can downgrade it to the previous version by click "Install another version" in the Intelephense extension. There are no errors on version 1.2.3.

Solution 14 - Php

Had same problem in v1.7.1. It was showing error on built-in functions. But just found the solution: go to extension setting @ext:bmewburn.vscode-intelephense-client and disable one by one Intelephense›Diagnostics: and you will see the error showing will stop.

Solution 15 - Php

For anyone going through these issues and uneasy about disabling a whole set of checks, there is a way to pass your own custom signatures to Intelephense.

Copied from Intelephese repo's comment (by @KapitanOczywisty):
https://github.com/bmewburn/vscode-intelephense/issues/892#issuecomment-565852100

> For single workspace it is very simple, you have to create .php file > with all signatures and intelephense will index them. > > If you want add stubs globally, you still can, but I'm not sure if > it's intended feature. Even if intelephense.stubs throws warning about > incorrect value you can in fact put there any folder name. > > json > { > "intelephense.stubs": [ > // ... > "/path/to/your/stub" > ] > } > > Note: stubs are refreshed with this setting change. > > You can take a look at build-in stubs here: > https://github.com/JetBrains/phpstorm-stubs

In my case, I needed dspec's describe, beforeEach, it... to don't be highlighted as errors, so I just included the file with the signatures /directories_and_paths/app/vendor/bin/dspec in my VSCode's workspace settings, which had the function declarations I needed:

function describe($description = null, \Closure $closure = null) {
}

function it($description, \Closure $closure) {
}

// ... and so on

Solution 16 - Php

I had the same issue and the following seemed to have addressed the issue.

a) Updated to latest version 1.3.5 and re-enabled all the diagnosis settings.

I was still getting the messages

b) Added the vendor folder with the dependent libraries to the workspace

This seems to have solved the problem.

Solution 17 - Php

 use Illuminate\Support\Facades\Route;

Add the above Namespace

Solution 18 - Php

In your web.php

Add this line of code

use Illuminate\Support\Facades\Route;

Then you are done, again if you got Auth error then add this line of code

use Illuminate\Support\Facades\Auth;

Thanks.

Solution 19 - Php

I recently fixed my own issue. by going file > preferences and I search for intelliphense The section that has file to exclude, I noticed vendor folder was added. I removed it, now all my laravel files are indexed

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdrian Edy PratamaView Question on Stackoverflow
Solution 1 - PhpbmewburnView Answer on Stackoverflow
Solution 2 - PhpRobin1990View Answer on Stackoverflow
Solution 3 - Phpuser12483351View Answer on Stackoverflow
Solution 4 - PhpcdcdcdView Answer on Stackoverflow
Solution 5 - PhprogervilaView Answer on Stackoverflow
Solution 6 - PhpNachoView Answer on Stackoverflow
Solution 7 - PhpSnapeyView Answer on Stackoverflow
Solution 8 - PhpSouljackerView Answer on Stackoverflow
Solution 9 - PhpVarolView Answer on Stackoverflow
Solution 10 - PhpElia WeissView Answer on Stackoverflow
Solution 11 - PhpAndy SongView Answer on Stackoverflow
Solution 12 - PhpQuentiumYTView Answer on Stackoverflow
Solution 13 - PhpexynaView Answer on Stackoverflow
Solution 14 - PhpHashirView Answer on Stackoverflow
Solution 15 - PhpjpennaView Answer on Stackoverflow
Solution 16 - PhpanoopjohnView Answer on Stackoverflow
Solution 17 - PhpKhn RzkView Answer on Stackoverflow
Solution 18 - PhpY. Joy Ch. SinghaView Answer on Stackoverflow
Solution 19 - PhpStanley AlohView Answer on Stackoverflow