274

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?

Adrian Edy Pratama
  • 3,841
  • 2
  • 9
  • 24
  • I have reported an issue on this at the issue queue of the application - https://github.com/bmewburn/vscode-intelephense/issues/885 – anoopjohn Dec 13 '19 at 22:46
  • If you're getting undefined error on Route in your `api.php`, see @user12483351's answer below. That fixed it for me. I'm on Intelephese 1.3.6. – dotNET Dec 28 '19 at 14:15

23 Answers23

262

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.

bmewburn
  • 3,726
  • 1
  • 12
  • 14
  • Passed a few days with annoying alerts, now with recent update 1.3.3 - it's all ok.. Thanks @bmewburn – Tariqul Islam Dec 10 '19 at 18:22
  • 8
    Thanks for separate config, for now I disabled undefinedMethods as intelephense can't detect laravel helper `auth()->user()` as class \App\User so calling method inside \App\User from `auth()->user()` will be detected as error. I just added `use Illuminate\Support\Facades\Route;` in my routes to fix undefined symbol route error. – Christhofer Natalius Dec 13 '19 at 08:49
  • 8
    Thanks for this. For my setup, just diable `intelephense.diagnostics.undefinedMethods` and `intelephense.diagnostics.undefinedType` works. – kamudrikah Dec 16 '19 at 02:59
  • `laravel-ide-helper` fixed the warning. Thank you! – giovannipds Jan 02 '20 at 14:19
  • Just needed to generate the `_ide_helper.php` as suggested on docs. – giovannipds Jan 02 '20 at 14:38
  • For me, issue is gone after upgrading to 1.3.7 without any additional configuration. Thanks for your great work. – Sir_Faenor Jan 14 '20 at 09:54
  • [laravel ide helper](https://github.com/barryvdh/laravel-ide-helper) solved the problem for me. just install it using composer and do `php artisan ide-helper:generate` – gema Jan 18 '20 at 09:22
  • 6
    Press `ctrl+,` in VsCode. Search for `undefined` and uncheck all checkboxes where it says `Intellephense > Diagnostics` – Hamza Waleed Feb 04 '20 at 10:40
171

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

Robin1990
  • 1,717
  • 2
  • 11
  • 13
  • 3
    Adding "use Illuminate\Support\Facades\Route;" will actually solve the problem. As a general rule, for each symbol not found, just declare the approriate Facade. – AlexWebLab Dec 09 '19 at 08:24
  • 3
    @Alex There are other problems. You are gonna get errors for query scopes etc. This is not just facades. – Miloslav Milo Janoušek Dec 09 '19 at 17:25
  • 2
    Intelephense 1.3.3 adds further config options to suit your workspace and coding style. Each category of undefined diagnostic can now be enabled/disabled to suit. – bmewburn Dec 10 '19 at 12:15
  • In my opinion, the best resolution would be to upgrade to the latest version 1.3.11 (as of now) and then disable the undefined types checks in your settings file `"intelephense.diagnostics.undefinedTypes": false,`. This way, you do not get to lose all the other fixes implemented in the new release. – Kalema Edgar Apr 21 '20 at 00:33
  • 1
    intelephense 1.4.0 also has an issue with referencing words like "Controller" and "View". I rolled back to 1.3.11 and it works fine. – KD_Raj May 30 '20 at 13:58
  • FANTASTIC, you help me – Carlos Maemo Jan 21 '21 at 06:19
67

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

Snapey
  • 3,604
  • 1
  • 21
  • 19
  • 1
    This appeared to work, but the next time I edited and saved my file it reappeared. :-( – Circle B Sep 02 '21 at 12:42
  • 4
    Just found my own comment in answer to my problem ! I had forgotten this.. – Snapey Sep 08 '21 at 11:18
  • This worked for me as intellisense had not yet discovered my newly installed library. On some Macs it's `command-shift-p`. For more on the command pallete you can refer to: https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette – Justin Feb 02 '23 at 15:37
  • This actually worked. I already had the project created and later installed packages but VSCode didn't seems to recognised until I see the `Index Workspace` – danish-khan-I Mar 16 '23 at 13:03
  • It is worked for me! – Abdüssamet Aslan Jul 02 '23 at 22:37
53
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
mightyteja
  • 825
  • 1
  • 14
  • 38
user12483351
  • 555
  • 3
  • 4
39

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
rogervila
  • 974
  • 1
  • 10
  • 27
  • 3
    I also had to remove `"**/vendor/**/{Tests,tests}/**",` ; no idea how that line got in there. – knb Oct 09 '20 at 14:01
  • 2
    I've done the same thing but still I'm getting error – Mohammad_Hosseini Dec 06 '20 at 17:39
  • 1
    @knb It's part of the default setting. The default contains `**/vendor/**/vendor/**` and `**/vendor/**/{Tests,tests}/**` – mbomb007 Aug 10 '22 at 13:28
  • @mohammad_hosseini try opening the folder into VSCode before attempting to open the script individually. – Paulo Costa Dec 19 '22 at 14:21
  • i guess intelephense includes these folders by default, im not sure why, removing the vendor references from Intelephense › Files: Exclude fixed type hinting for me without installing ide-helper. do not remove them from Intelephense › References: Exclude or Intelephense › Rename: Exclude. – Rtzoor Mar 19 '23 at 13:04
29

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

cdcdcd
  • 1,612
  • 13
  • 18
  • 1
    The probelm with this is you get duplicated definitions – Carlos Mora Dec 04 '19 at 17:16
  • intelephense stopped highlighting `Auth` after I restarted VS Code. I did exactly the steps shown in this answer. After restarting VS Code, give it a minute or two and the errors should clear. – agm1984 May 03 '20 at 20:49
26

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

Nacho
  • 628
  • 1
  • 12
  • 19
  • 3
    IDE Helper does solve the problems with Route closure or other closures, but you will run across other errors while using eloquent scopes etc. – Miloslav Milo Janoušek Dec 03 '19 at 13:17
  • 11
    So to solve an issue we need to loose true diagnostics. No way Josei! – Carlos Mora Dec 04 '19 at 17:15
  • 1
    I installed IDE Helper and generated the file as per instructions.. this is on a Laravel project. It appeared to change nothing and still had undefined symbols all over the place. So I downgraded to 1.2.3 – vesperknight Dec 05 '19 at 21:25
  • 1
    Thanks for this advice - it seems now there is an even easier option - if you click on the cog for settings and scroll through the extensions settings for intelliphense there are various tick boxes and one is for undefined types. I was having so many errors of undefined types in my code as I'm working on a joomla template and I don't think intelliphense could see the various Joomla classes like JRequest, JFactory, etc... one addition that would be really useful is to tell it to only ignore undefined types that match a certain pattern (like in Joomla start with the letter J) – TheKLF99 Mar 15 '21 at 06:20
16

1.3.1 fixed it.

Just update your extension and you should be good to go

Souljacker
  • 595
  • 6
  • 23
16

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

Quentium
  • 335
  • 1
  • 5
  • 16
14

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

Hef
  • 606
  • 6
  • 16
13

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
Varol
  • 1,798
  • 1
  • 22
  • 30
  • 1
    Is there a "procedure" to find proper "disagnostics" for a given error message? Say, I have a legacy project and there are lots of messages like "Non static method 'builder' should not be called statically.intelephense(1036)". Which of options should disable these messages? – pilat Jun 12 '20 at 07:25
11

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
Elia Weiss
  • 8,324
  • 13
  • 70
  • 110
6

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.

Andy Song
  • 4,404
  • 1
  • 11
  • 29
5

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
exyna
  • 67
  • 3
5

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

ODelibalta
  • 2,194
  • 1
  • 18
  • 28
3

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.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
3

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.

Hashir
  • 95
  • 7
2

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
jpenna
  • 8,426
  • 5
  • 28
  • 36
2
 use Illuminate\Support\Facades\Route;

Add the above Namespace

Khn Rzk
  • 1,124
  • 2
  • 15
  • 26
  • 1
    @francisco how does declaring a class alias violate a PSR standard? Which PSR? – miken32 Dec 28 '21 at 15:58
  • @francisco "above" is referencing the declaration in the example. In other words, "Add the namespace, as shown above." – miken32 Dec 28 '21 at 16:29
  • Regardless, this answer was [already given](https://stackoverflow.com/a/59187744/1255289) the day after the question was asked... – miken32 Dec 28 '21 at 16:37
1

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

Ahsan Najam
  • 155
  • 1
  • 8
0

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.

anoopjohn
  • 518
  • 4
  • 18
0

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

Stanley Aloh
  • 360
  • 4
  • 11
0

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

Enjoy!