Adrian Edy Pratama
Adrian Edy Pratama

Reputation: 4041

Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

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?

Upvotes: 294

Views: 392456

Answers (25)

enrique
enrique

Reputation: 144

Settings > intelephense.stubs >Add Item > select ds > ok

This one worked for me. Here's the source: https://github.com/bmewburn/vscode-intelephense/issues/2347#issuecomment-1312397169

Upvotes: 1

bmewburn
bmewburn

Reputation: 4036

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": false,
    "intelephense.diagnostics.undefinedFunctions": false,
    "intelephense.diagnostics.undefinedConstants": false,
    "intelephense.diagnostics.undefinedClassConstants": false,
    "intelephense.diagnostics.undefinedMethods": false,
    "intelephense.diagnostics.undefinedProperties": false,
    "intelephense.diagnostics.undefinedVariables": true,

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.

Upvotes: 281

gdm
gdm

Reputation: 7978

I just closed and reopened the VS. And now Itelliphense does not complain. This procedure is called: Shut-down And Turn-on (SAT) :)

Upvotes: 1

Harst Logics
Harst Logics

Reputation: 29

In my case, I had to deactivate the lastly installed extension, then deactivate Intelephense and reactivate it, and it worked for me.

Enjoy!

Upvotes: 0

Hef
Hef

Reputation: 1500

In my case I had to force Intelephense to index the workspace. Here's how I did it, in VsCode:

ctrl + shift + p > Intelephense: Index workspace

Upvotes: 28

ODelibalta
ODelibalta

Reputation: 2254

I hope this helps anyone. I use it often. This will make your vscode "restart" without actually restarting. Part of this process will make your plugins update their cached indexes. cmd+shift+p or ctrl+shift+p

enter image description here

Upvotes: 5

Ahsan Najam
Ahsan Najam

Reputation: 363

The only working solution I found is:

Set language mode to Blade (use extension: Laravel Blade formatter) It will resolve the issue. Otherwise, follow this procedure.

These classes don't exist in the workspace. Laravel creates them at runtime. As such they are reported as undefined. The solution is to either provide stub definitions

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

or turn off the diagnostics (intelephense.diagnostics.undefinedTypes).

Upvotes: 1

Stanley Aloh
Stanley Aloh

Reputation: 437

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

Upvotes: 0

Quentium
Quentium

Reputation: 394

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

Upvotes: 19

Hashir
Hashir

Reputation: 95

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.

Upvotes: 3

Y. Joy Ch. Singha
Y. Joy Ch. Singha

Reputation: 3260

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.

Upvotes: 3

khn Rzk
khn Rzk

Reputation: 1292

 use Illuminate\Support\Facades\Route;

Add the above Namespace

Upvotes: 2

rogervila
rogervila

Reputation: 1064

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

Upvotes: 47

jpenna
jpenna

Reputation: 9211

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.

{   
   "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

Upvotes: 2

cdcdcd
cdcdcd

Reputation: 1722

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

Upvotes: 32

user12483351
user12483351

Reputation: 555

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

Upvotes: 53

Varol
Varol

Reputation: 1858

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

Upvotes: 13

Snapey
Snapey

Reputation: 4110

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

Upvotes: 88

Elia Weiss
Elia Weiss

Reputation: 10040

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

Upvotes: 13

anoopjohn
anoopjohn

Reputation: 518

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.

Upvotes: 0

Souljacker
Souljacker

Reputation: 595

1.3.1 fixed it.

Just update your extension and you should be good to go

Upvotes: 16

exyna
exyna

Reputation: 67

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.

Upvotes: 5

Nacho
Nacho

Reputation: 658

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

Upvotes: 26

Robin1990
Robin1990

Reputation: 1745

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

Upvotes: 173

Andy Song
Andy Song

Reputation: 4684

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.

Upvotes: 6

Related Questions