Visual Studio Code PHP Intelephense 不断显示不必要的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/59149877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:36:32  来源:igfitidea点击:

Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error

phplaravelvisual-studio-code

提问by Adrian Edy Pratama

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.

在我今天获得 PHP Intelephense 的最新更新之后,intelephense 不断显示我的路线(以及其他类)的未定义符号错误,之前没有这样的错误,这让我很困扰。

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?

实际上这段代码没有错误,但是智能电话一直显示错误,所以有没有办法解决这个问题?

采纳答案by bmewburn

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.

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

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

https://github.com/barryvdh/laravel-ide-helper这样的存根生成器帮助填补了这里的空白,将它与 Laravel 一起使用将通过提供易于发现的具体符号定义来处理许多错误诊断。

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.

尽管如此,PHP 是一种非常灵活的语言,根据代码的编写方式,可能还有其他虚假未定义符号的实例。出于这个原因,自 1.3.3 起,intelephense 具有配置选项来启用/禁用每个类别的未定义符号以适应工作区和编码风格。

These options are: intelephense.diagnostics.undefinedTypesintelephense.diagnostics.undefinedFunctionsintelephense.diagnostics.undefinedConstantsintelephense.diagnostics.undefinedClassConstantsintelephense.diagnostics.undefinedMethodsintelephense.diagnostics.undefinedPropertiesintelephense.diagnostics.undefinedVariables

这些选项是: intelephense.diagnostics.undefinedTypesintelephense.diagnostics.undefinedFunctionsintelephense.diagnostics.undefinedConstantsintelephense.diagnostics.undefinedClassConstantsintelephense.diagnostics.undefinedMethodsintelephense.diagnostics.undefinedPropertiesintelephense.diagnostics.undefinedVariables

Setting all of these to false except intelephense.diagnostics.undefinedVariableswill give version 1.2 behaviour. See the VSCode settings UI and search for intelephense.

将所有这些设置为 false exceptintelephense.diagnostics.undefinedVariables将提供 1.2 版行为。查看 VSCode 设置 UI 并搜索intelephense.

回答by Robin1990

Version 1.3.0 has flaw IMO.
Downgrade to version 1.2.3 fixes my problem.

I'm on

版本 1.3.0 有缺陷 IMO。
降级到 1.2.3 版修复了我的问题。

我上线了

  • Laravel 5.1
  • PHP 5.6.40
  • Laravel 5.1
  • PHP 5.6.40

Downgrade to Version 1.2.3

降级到 1.2.3 版

回答by Nacho

You don't need to downgrade you can:

您无需降级即可:

Either disable undefined symbol diagnostics in the settings -- "intelephense.diagnostics.undefinedSymbols": false .

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

Or use an ide helper that adds stubs for laravel facades. See https://github.com/barryvdh/laravel-ide-helper

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

回答by user12483351

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
  • 幼虫 6+
  • vscode 版本 1.40.2
  • php 电话 1.3.1

回答by SoulHymaner

1.3.1 fixed it.

1.3.1 修复它。

Just update your extension and you should be good to go

只需更新您的扩展程序,您就可以开始使用了

回答by AC Patrice

This solution may help you if you know your problems are limited to Facades and you are running Laravel 5.5 or above.

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

Install laravel-ide-helper

安装 laravel-ide-helper

composer require --dev barryvdh/laravel-ide-helper

composer require --dev barryvdh/laravel-ide-helper

Add this conditional statement in your AppServiceProviderto register the helper class.

在您AppServiceProvider的注册助手类中添加此条件语句。

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

Then run php artisan ide-helper:generateto generate a file to help the IDE understand Facades. You will need to restart Visual Studio Code.

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

References

参考

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

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

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

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

回答by Andy Song

This is really a set of configurations for your editor to understand Laravel.

这真的是一组配置,让你的编辑器了解 Laravel。

If you want to configure it all manually, here is the repo. This is for both VS code and PhpStorm.

如果你想手动配置它,这里是repo。这适用于 VS 代码和 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.

然后就跑andylaravel setupIDE。这将根据 fist repo 为您配置一切。

回答by exyna

No, the errors occurs only after the Intelephense extension is automatically updated.

不,只有在 Intelephense 扩展自动更新后才会出现错误。

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.

要解决此问题,您可以通过单击 Intelephense 扩展中的“安装另一个版本”将其降级到以前的版本。版本 1.2.3 上没有错误。

回答by Snapey

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

如果您在添加新的 Vendor 类后立即看到此信息,请务必运行 VScode 命令 (control-shift-P) Index Workspace

回答by Elia Weiss

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