Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error
Solution 1
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
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
Solution 3
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
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
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
Related videos on Youtube
Adrian Edy Pratama
A student who goes to a vocational high school of software engineering and now studying at technopreneur college while doing a part-time job as a web developer.
Updated on January 23, 2022Comments
-
Adrian Edy Pratama over 2 years
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 :
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?
-
anoopjohn over 4 yearsI have reported an issue on this at the issue queue of the application - github.com/bmewburn/vscode-intelephense/issues/885
-
dotNET over 4 yearsIf 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.
-
-
Miloslav Milo Janoušek over 4 yearsIDE Helper does solve the problems with Route closure or other closures, but you will run across other errors while using eloquent scopes etc.
-
tonix over 4 yearsMe too, is there a fix or should we downgrade?
-
Carlos Mora over 4 yearsFor me it doesn't solves anything. Going back to 1.2.3 goes fine!
-
Carlos Mora over 4 yearsSo to solve an issue we need to loose true diagnostics. No way Josei!
-
Carlos Mora over 4 yearsThe probelm with this is you get duplicated definitions
-
NULL pointer over 4 years1.3.1 still has this problem for me. 1.2.3 fixed it
-
vesperknight over 4 yearsI 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
-
AlexWebLab over 4 yearsI'm on 1.3.2 and still NOT fixed. Re-downgraded to 1.2.3.
-
AlexWebLab over 4 yearsThis actually solves the problem. For commands inside console.php it works as well with: use Illuminate\Support\Facades\Artisan;
-
AlexWebLab over 4 yearsAdding "use Illuminate\Support\Facades\Route;" will actually solve the problem. As a general rule, for each symbol not found, just declare the approriate Facade.
-
Miloslav Milo Janoušek over 4 years@Alex There are other problems. You are gonna get errors for query scopes etc. This is not just facades.
-
bmewburn over 4 yearsIntelephense 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.
-
Tariqul Islam over 4 yearsPassed a few days with annoying alerts, now with recent update 1.3.3 - it's all ok.. Thanks @bmewburn
-
Christhofer Natalius over 4 yearsThanks 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 fromauth()->user()
will be detected as error. I just addeduse Illuminate\Support\Facades\Route;
in my routes to fix undefined symbol route error. -
kamudrikah over 4 yearsThanks for this. For my setup, just diable
intelephense.diagnostics.undefinedMethods
andintelephense.diagnostics.undefinedType
works. -
giovannipds over 4 years
laravel-ide-helper
fixed the warning. Thank you! -
giovannipds over 4 yearsThis is not laravel recommended.
laravel-ide-helper
configuration is. -
giovannipds over 4 yearsYou could use
laravel-ide-helper
-
giovannipds over 4 yearsJust needed to generate the
_ide_helper.php
as suggested on docs. -
anoopjohn over 4 yearsThanks for the tip. I am using Symfony inside Drupal. Would laravel-ide-helper help?
-
Sir_Faenor over 4 yearsFor me, issue is gone after upgrading to 1.3.7 without any additional configuration. Thanks for your great work.
-
gema over 4 yearslaravel ide helper solved the problem for me. just install it using composer and do
php artisan ide-helper:generate
-
Daydah over 4 yearsAdding this solved the issue for
Route
, but I had the same error forAuth
, so I addeduse Illuminate\Support\Facades\Auth;
as well. Problem solved. -
Hamza Waleed over 4 yearsPress
ctrl+,
in VsCode. Search forundefined
and uncheck all checkboxes where it saysIntellephense > Diagnostics
-
Kalema Edgar about 4 yearsIn 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. -
agm1984 about 4 yearsintelephense 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. -
KD_Raj almost 4 yearsintelephense 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.
-
pilat almost 4 yearsIs 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?
-
Zombie Chibi XD almost 4 years...
(I use VSCode, but it should work the same for Visual Studio Code)
... Huh? -
jpenna almost 4 yearsYeah, messed up the names, thought the question was about Visual Studio
-
pkucas over 3 years@Daydah same for me
-
asela daskon over 3 yearsafter trying this solution, I reset the "Index Workspace" thanks...
-
knb over 3 yearsI also had to remove
"**/vendor/**/{Tests,tests}/**",
; no idea how that line got in there. -
Mohammad_Hosseini over 3 yearsdoes not work on intelephense version 1.5.4, any fixes?
-
Mohammad_Hosseini over 3 yearsI've done the same thing but still I'm getting error
-
Carlos Maemo over 3 yearsFANTASTIC, you help me
-
TheKLF99 about 3 yearsThanks 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)
-
Circle B over 2 yearsThis appeared to work, but the next time I edited and saved my file it reappeared. :-(
-
Snapey over 2 yearsJust found my own comment in answer to my problem ! I had forgotten this..
-
Grant over 2 yearsWhere do you go to edit this?
-
QuentiumYT over 2 yearsYou can add these rules inside the settings.json file. Press F1 and look for "Open user settings (JSON)"
-
Grant over 2 yearsThanks @QuentiumYT - $HOME/Library/Application Support/Code/User/settings.json
-
francisco over 2 yearsThis don't follow PSR standard
-
miken32 over 2 years@francisco how does declaring a class alias violate a PSR standard? Which PSR?
-
francisco over 2 years@miken32 When present, all
use
declarations MUST go after thenamespace
declaration. (and not above) -
miken32 over 2 years@francisco "above" is referencing the declaration in the example. In other words, "Add the namespace, as shown above."
-
miken32 over 2 yearsRegardless, this answer was already given the day after the question was asked...
-
oussama benounnas over 2 yearsthank you! for my case, i tried the
laravel-ide-helper
, i just deactivatedintelephense.diagnostics.undefinedMethods
because my livewire component was going crazy -
Stu over 2 yearsThis worked for me, after trying all the above suggestions with no luck. Thanks @QuentiumYT