简体   繁体   English

PhpStorm静态方法代码完成

[英]PhpStorm static method code completion

I use Laravel ORM in my php project and I use a lot of dynamic where in my code. 我在我的php项目中使用Laravel ORM,并且在我的代码中使用了很多动态。 Eg 例如

UserModel::whereName('Foo')->whereGender(1)->first();

When I use PhpStorm 2016.1 (I think) I can add PHPDoc string to class UserModel to have the code completion with whereName and the following whereGender by doing the following: 当我使用PhpStorm 2016.1(我认为)时,我可以将PHPDoc字符串添加到类UserModel以通过执行以下whereGender来使用whereName和以下whereGender完成代码:

/**
* class UserModel
* @method static $this whereName($val)
* @method static $this whereGender($val)
*/
class UserModel {
 .......
}

But after I upgrade to the newest version of PhpStorm 2016.2.2. 但在我升级到PhpStorm 2016.2.2的最新版本之后。 the second method whereGender will not appear in the code completion list. 第二种方法whereGender不会出现在代码完成列表中。 On JetBrains website I found the 2016.2's new feature 在JetBrains网站上,我找到了2016.2的新功能

static methods are filtered out from completion lists. 静态方法从完成列表中过滤掉。

My question is: is there a way to make PhpStorm give the completion whereGender after the whereName call? 我的问题是:有没有办法使得PhpStorm在whereName调用之后给出完成whereGender

update: 更新:

Completion has to be invoked twice to get static methods. 必须调用两次完成才能获得静态方法。 https://youtrack.jetbrains.com/issue/WI-9403#comment=27-1490430 https://youtrack.jetbrains.com/issue/WI-9403#comment=27-1490430

tl;dr : tl;博士

To see the completion, after you type -> , you have to press CTRL + SPACE to see the complete completion list with the static methods. 要查看完成,在键入-> ,必须按CTRL + SPACE才能使用静态方法查看完整的完成列表。 If you are on a mac and it conflicts with your language toggle, change the key combo at: 如果您使用的是Mac并且与语言切换冲突,请更改以下组件的键组合:

Preferences->Keymap:
Main Menu->Code->Completion->Basic

While there is a real use case for this, calling a method static/non-static is not common, hence phpstorm dropped it, as mentioned it here: https://blog.jetbrains.com/phpstorm/2016/07/completion-changes-in-phpstorm/ 虽然有一个真正的用例,但调用静态/非静态方法并不常见,因此phpstorm放弃了它,如下所述: https ://blog.jetbrains.com/phpstorm/2016/07/completion- 变化功能于phpstorm /

The next completion change was to remove static methods from the completion list when completion is invoked in the context of $this-> . 下一个完成更改是在$this->的上下文中调用完成时从完成列表中删除静态方法。 This change happened as a result of a ticket opened by a user a few years ago. 这种变化发生在几年前用户打开的票证的结果。 Calling static methods using $this-> is an entirely valid use case but is not that widely used in PHP. 使用$this->调用静态方法是一个完全有效的用例,但在PHP中并没有广泛使用。 While this seemed like a good idea at the time, it's had the unintended consequences of making writing assertions in PHPUnit a little more painful. 虽然这在当时看起来是一个好主意,但它在PHPUnit中编写断言会产生一些意想不到的后果。

So regardless how you write the phpdoc, phpstorm will not show the static methods as instance methods on the "first invocation". 所以无论你如何编写phpdoc,phpstorm都不会在“第一次调用”中将静态方法显示为实例方法。 You will need to manually invoke it again (using CTRL+Space) to see it. 您需要再次手动调用它(使用CTRL + Space)来查看它。 So this is what the "second invocation" means. 所以这就是“第二次调用”的意思。

Sources: 资料来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM