在我今天得到的PHP Intelephense的最新更新之后,Intelephense一直显示我的路由(和其他类)的未定义符号的错误,以前没有这样的错误,它困扰着我。

以下是错误截图:

这是我的代码:

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');
});

实际上,这段代码中没有错误,但intelephense一直显示错误,所以有办法解决这个问题吗?


当前回答

如果您知道您的问题仅限于Facades,并且您正在运行Laravel 5.5或更高版本,那么这个解决方案可能会对您有所帮助。

安装laravel-ide-helper

编译器需要——dev barryvdh/ laravell -ide-helper

在AppServiceProvider中添加这个条件语句来注册helper类。

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

然后运行php artisan IDE -helper:generate生成一个文件来帮助IDE理解Facades。您需要重新启动Visual Studio Code。

参考文献

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

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

其他回答

这实际上是一组用于编辑器理解Laravel的配置。

如果您想手动配置它,这里是repo。这是为vscode和PhpStorm。

或者如果你愿意,你可以下载这个包。(我创建的)建议全局安装。

然后运行andylaravel setupIDE。这将根据第一个repo为您配置所有内容。

对于遇到这些问题并对禁用一整套检查感到不安的人来说,有一种方法可以将您自己的自定义签名传递给Intelephense。

摘自Intelephese repo的评论(来自@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

在我的情况下,我需要dspec的描述,beforeEach,它…不被突出显示为错误,所以我只是包括签名/ directores_and_paths /app/vendor/bin/dspec文件在我的VSCode的工作空间设置,其中有我需要的函数声明:

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

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

// ... and so on

你不需要降级,你可以:

在设置中禁用未定义的符号诊断——"intelephense.diagnostics. "undefinedSymbols": false。

或者使用ide助手为laravel外观添加存根。参见https://github.com/barryvdh/laravel-ide-helper

我找到的唯一可行的解决方案是:

设置语言模式为Blade(使用扩展:Laravel Blade格式化器) 这将解决问题。否则,请按照此步骤操作。

这些类不存在于工作区中。Laravel在运行时创建它们。因此,它们被报告为未定义的。解决方案是提供存根定义

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

或关闭诊断(intelephense.diagnostics.undefinedTypes)。

下面是我解决的问题:

打开扩展设置:

然后搜索你想要更改的变量,然后取消选中它

你应该考虑的变量有:

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