Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

227,806

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

Downgrade to Version 1.2.3

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

Share:
227,806

Related videos on Youtube

Adrian Edy Pratama
Author by

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, 2022

Comments

  • Adrian Edy Pratama
    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 :

    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?

    • anoopjohn
      anoopjohn over 4 years
      I have reported an issue on this at the issue queue of the application - github.com/bmewburn/vscode-intelephense/issues/885
    • dotNET
      dotNET over 4 years
      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.
  • Miloslav Milo Janoušek
    Miloslav Milo Janoušek over 4 years
    IDE Helper does solve the problems with Route closure or other closures, but you will run across other errors while using eloquent scopes etc.
  • tonix
    tonix over 4 years
    Me too, is there a fix or should we downgrade?
  • Carlos Mora
    Carlos Mora over 4 years
    For me it doesn't solves anything. Going back to 1.2.3 goes fine!
  • Carlos Mora
    Carlos Mora over 4 years
    So to solve an issue we need to loose true diagnostics. No way Josei!
  • Carlos Mora
    Carlos Mora over 4 years
    The probelm with this is you get duplicated definitions
  • NULL pointer
    NULL pointer over 4 years
    1.3.1 still has this problem for me. 1.2.3 fixed it
  • vesperknight
    vesperknight over 4 years
    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
  • AlexWebLab
    AlexWebLab over 4 years
    I'm on 1.3.2 and still NOT fixed. Re-downgraded to 1.2.3.
  • AlexWebLab
    AlexWebLab over 4 years
    This actually solves the problem. For commands inside console.php it works as well with: use Illuminate\Support\Facades\Artisan;
  • AlexWebLab
    AlexWebLab over 4 years
    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.
  • Miloslav Milo Janoušek
    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
    bmewburn over 4 years
    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.
  • Tariqul Islam
    Tariqul Islam over 4 years
    Passed a few days with annoying alerts, now with recent update 1.3.3 - it's all ok.. Thanks @bmewburn
  • Christhofer Natalius
    Christhofer Natalius over 4 years
    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.
  • kamudrikah
    kamudrikah over 4 years
    Thanks for this. For my setup, just diable intelephense.diagnostics.undefinedMethods and intelephense.diagnostics.undefinedType works.
  • giovannipds
    giovannipds over 4 years
    laravel-ide-helper fixed the warning. Thank you!
  • giovannipds
    giovannipds over 4 years
    This is not laravel recommended. laravel-ide-helper configuration is.
  • giovannipds
    giovannipds over 4 years
    You could use laravel-ide-helper
  • giovannipds
    giovannipds over 4 years
    Just needed to generate the _ide_helper.php as suggested on docs.
  • anoopjohn
    anoopjohn over 4 years
    Thanks for the tip. I am using Symfony inside Drupal. Would laravel-ide-helper help?
  • Sir_Faenor
    Sir_Faenor over 4 years
    For me, issue is gone after upgrading to 1.3.7 without any additional configuration. Thanks for your great work.
  • gema
    gema over 4 years
    laravel ide helper solved the problem for me. just install it using composer and do php artisan ide-helper:generate
  • Daydah
    Daydah over 4 years
    Adding this solved the issue for Route, but I had the same error for Auth, so I added use Illuminate\Support\Facades\Auth; as well. Problem solved.
  • Hamza Waleed
    Hamza Waleed over 4 years
    Press ctrl+, in VsCode. Search for undefined and uncheck all checkboxes where it says Intellephense > Diagnostics
  • Kalema Edgar
    Kalema Edgar about 4 years
    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.
  • agm1984
    agm1984 about 4 years
    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.
  • KD_Raj
    KD_Raj almost 4 years
    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.
  • pilat
    pilat almost 4 years
    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?
  • Zombie Chibi XD
    Zombie Chibi XD almost 4 years
    ...(I use VSCode, but it should work the same for Visual Studio Code)... Huh?
  • jpenna
    jpenna almost 4 years
    Yeah, messed up the names, thought the question was about Visual Studio
  • pkucas
    pkucas over 3 years
    @Daydah same for me
  • asela daskon
    asela daskon over 3 years
    after trying this solution, I reset the "Index Workspace" thanks...
  • knb
    knb over 3 years
    I also had to remove "**/vendor/**/{Tests,tests}/**", ; no idea how that line got in there.
  • Mohammad_Hosseini
    Mohammad_Hosseini over 3 years
    does not work on intelephense version 1.5.4, any fixes?
  • Mohammad_Hosseini
    Mohammad_Hosseini over 3 years
    I've done the same thing but still I'm getting error
  • Carlos Maemo
    Carlos Maemo over 3 years
    FANTASTIC, you help me
  • TheKLF99
    TheKLF99 about 3 years
    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)
  • Circle B
    Circle B over 2 years
    This appeared to work, but the next time I edited and saved my file it reappeared. :-(
  • Snapey
    Snapey over 2 years
    Just found my own comment in answer to my problem ! I had forgotten this..
  • Grant
    Grant over 2 years
    Where do you go to edit this?
  • QuentiumYT
    QuentiumYT over 2 years
    You can add these rules inside the settings.json file. Press F1 and look for "Open user settings (JSON)"
  • Grant
    Grant over 2 years
    Thanks @QuentiumYT - $HOME/Library/Application Support/Code/User/settings.json
  • francisco
    francisco over 2 years
    This don't follow PSR standard
  • miken32
    miken32 over 2 years
    @francisco how does declaring a class alias violate a PSR standard? Which PSR?
  • francisco
    francisco over 2 years
    @miken32 When present, all use declarations MUST go after the namespace declaration. (and not above)
  • miken32
    miken32 over 2 years
    @francisco "above" is referencing the declaration in the example. In other words, "Add the namespace, as shown above."
  • miken32
    miken32 over 2 years
    Regardless, this answer was already given the day after the question was asked...
  • oussama benounnas
    oussama benounnas over 2 years
    thank you! for my case, i tried the laravel-ide-helper, i just deactivated intelephense.diagnostics.undefinedMethods because my livewire component was going crazy
  • Stu
    Stu over 2 years
    This worked for me, after trying all the above suggestions with no luck. Thanks @QuentiumYT