在我今天得到的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一直显示错误,所以有办法解决这个问题吗?


当前回答

版本1.3.0有缺陷。 降级到1.2.3版本可以解决我的问题。 我在

Laravel 5.1 PHP 5.6.40

其他回答

Intelephense 1.3增加了未定义的类型、函数、常量、类常量、方法和属性诊断,而之前在1.2中只有未定义的变量诊断。

一些框架的编写方式为用户提供了方便的快捷方式,但使得静态分析引擎很难发现运行时可用的符号。

像https://github.com/barryvdh/laravel-ide-helper这样的存根生成器有助于填补这里的空白,并且通过提供易于发现的符号的具体定义,将其与Laravel一起使用将解决许多错误诊断问题。

不过,PHP是一种非常灵活的语言,根据代码的编写方式,可能会出现其他错误的未定义符号。因此,从1.3.3开始,intelephense有配置选项来启用/禁用每一类未定义符号,以适应工作空间和编码风格。

这些选项是: intelephense.diagnostics.undefinedTypes intelephense.diagnostics.undefinedFunctions intelephense.diagnostics.undefinedConstants intelephense.diagnostics.undefinedClassConstants intelephense.diagnostics.undefinedMethods intelephense.diagnostics.undefinedProperties intelephense.diagnostics.undefinedVariables

除了intelephense.diagnostics.undefinedVariables将给出1.2版本的行为外,将所有这些设置为false。查看VSCode设置界面并搜索intelephense。

对那些宁愿保持简单的人来说,愚蠢;如果你想摆脱通知,而不是安装帮助程序或降级,只需在设置中禁用错误。Json:

"intelephense.diagnostics.undefinedTypes": false
use Illuminate\Support\Facades\Route;

警告导入相应的命名空间后消失。

版本的

幼虫 6+ VSCODE 版本 1.40.2 PHP Intelephense 1.3.1

 use Illuminate\Support\Facades\Route;

添加上述命名空间

如果您知道您的问题仅限于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