简体   繁体   English

PhpStorm类型提示派生类返回值

[英]PhpStorm type hinting for derived class return value

Is it possible to hint PhpStorm that in the code below return value of item() method has the type DOMElement ( WITHOUT modifying the actual PHP statements , ie by some external setting of PhpStorm or plugin or using PHPDoc comment of some sort, etc.)? 是否有可能提示PhpStorm在下面的代码中返回item()方法的值具有类型DOMElement没有修改实际的PHP语句 ,即通过某些外部设置的PhpStorm或插件或使用某种类型的PHPDoc注释等) ?

Again, please, don't suggest me to modify the statements in the code , in particular the part $list->item(0)->getAttribute('test') . 再次,请不要建议我修改代码中的语句 ,特别是部分$list->item(0)->getAttribute('test')

Also, I found a somewhat similar question PhpStorm type-hinting for factories? 另外,我发现了一个类似的问题PhpStorm类型提示工厂? but it deals with type-hinting for user-defined functions, in my case the function item() is a predefined library function. 但它处理用户定义函数的类型提示,在我的例子中,函数item()是预定义的库函数。

$doc = new DOMDocument();
$doc->loadXML('<x test="123"/>');
$list = $doc->getElementsByTagName('x');
if($list->length > 0)
    var_dump($list->item(0)->getAttribute('test'));

PhpStorm警告

Accordingly to official docs $list->item(0) returns element of DOMNode class which does NOT have getAttribute() method. 相应于官方文档 $list->item(0)返回没有getAttribute()方法的DOMNode类的元素。 PhpStorm follows official docs here. PhpStorm遵循官方文档。

But if you add extra line into youe code and analyze it in debugger (eg $z = $list->item(0); ), you will see that $z is instance of DOMElement that has such method. 但是如果你在你的代码中添加额外的行并在调试器中进行分析(例如$z = $list->item(0); ),你会发现$z具有这种方法的DOMElement实例。


A. You cannot add documentation to predefined methods (especially in a chain) on the fly. 答:您无法动态地向预定义方法(尤其是链中)添加文档。 No way. 没门。

B. As you can see -- the "problem" here is in the docs: they say that item() returns instance of one class while in your particular case it returns instance of different class. B.正如您所看到的 - 这里的“问题”在于文档:他们说item()返回一个类的实例,而在您的特定情况下它返回不同类的实例。


Few options: 几个选项:

1. Modify your code to introduce intermediate variable to which you can give correct type hint. 1.修改代码以引入中间变量,您可以为其提供正确的类型提示。 As I understand you do not want to use it. 据我了解你不想使用它。

2. Ignore these warnings: you could disable it completely or for just that file; 2.忽略这些警告:您可以完全禁用它或只删除该文件; you could lower the severity (eg "info" instead of "warning") etc 你可以降低严重程度(例如“信息”而不是“警告”)等

3. Alter documentation (not sure if this is a good idea though). 3.改变文档(不确定这是否是一个好主意)。

This could be done on global level by modifying actual stub file (part of PhpStorm distribution... so it will affect ALL projects and you will have to redo this after upgrading to a newer version again). 这可以通过修改实际存根文件(PhpStorm发布的一部分......在全局范围内完成...因此它将影响所有项目,并且您必须在再次升级到更新版本后重做此项)。

Or you can do this on local level (for this project only): 或者您可以在本地级别执行此操作(仅适用于此项目):

  • extract stub for DOMNodeList class from original stub file 从原始存根文件中提取DOMNodeList类的存根
  • place it in separate file anywhere in your project 将它放在项目中任何位置的单独文件中
  • alter @return tag of item() method -- make it @return DOMElement of whatever you require there. 改变item()方法的@return标签 - 使它成为@return DOMElement ,无论你需要什么。

This will make IDE "merging" docs from two sources: sort of @return DOMNode|DOMElement . 这将使IDE从两个来源“合并”文档: @return DOMNode|DOMElement

Once again -- this will cover your particular case -- how this will affect your other cases I cannot tell. 再次 - 这将涵盖您的特定情况 - 这将如何影响您无法分辨的其他情况。

I'm not sure what exactly you mean by not being able to edit the PHP statements but at the same time being able to edit PHPDoc comments, but you can accomplish what you asked for by doing the following: 我不确定你是什么意思不能编辑PHP语句但同时能够编辑PHPDoc注释,但你可以通过执行以下操作来完成你所要求的:

<?php
$doc = new DOMDocument();
$doc->loadXML('
<x test="123"/>');
$list = $doc->getElementsByTagName('x');
if($list->length > 0) {
    /** @var DomElement $element */
    $element = $list->item(0);
    var_dump($element->getAttribute('test'));
}

But this is not technically safe since getElementsByTagName returns a DomNodeList which specifies that it contains DomNodes (although I'm not familiar enough to know if for all practical purposes the list will only contain DomElements) 但这在技术上并不安全,因为getElementsByTagName返回一个DomNodeList,它指定它包含DomNodes(虽然我不熟悉,不知道为了所有实际目的,列表是否只包含DomElements)

Either way, when I look at this example code, the methods being used are already properly documented, your issue is you're trying to specify, as stated above, special constraints that are not enforced (specifically) in the DOM code/comments. 无论哪种方式,当我查看此示例代码时,正在使用的方法已经正确记录,您的问题是您正在尝试指定(如上所述)在DOM代码/注释中未强制执行的特殊约束。

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

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