PHPStorm and magic methods

巧了我就是萌 提交于 2019-12-08 21:04:30

问题


I am using PHPStorm and have written a class that utilises the SimpleXML class. Everything is fine, except when I traverse an XML string I will get "Undefined Property" warnings.

$xml = simplexml_load_string($string); //Returns SimpleXML Element

echo $xml->childElement; //PHPStorm reports "Undefined Property

I believe this is because the magic properties are not properly defined in PHPStorm. Is anyone aware of a nice little work around? It annoys me because I am pedantic about having nice clean code (and IDE) and having warnings come up on a class is just awful!


回答1:


I have not found a work-around so far but just creating a type with the properties in question and var-type-hinting the variable:

class myXmlStoredValueObject {
     /* @var string */
     public $childElement;
}

$xml = simplexml_load_string($string); //Returns SimpleXML Element

/* @var $xml myXmlStoredValueObject */

echo $xml->childElement;

Naturally this is not always applicable / practicable, there is a cheat with stdClass:

$xml = simplexml_load_string($string); //Returns SimpleXML Element

/* @var $xml stdClass */

echo $xml->childElement;

You don't need to declare any concrete type to make the hint disappear.

There are other deficiencies, too, you will still run into issues withforeach e.g., where you need to var-type-hint again.




回答2:


I think I've solved it. If I encapsulate the nodes inside curly braces as strings, PHPStorm will ignore these.

echo $xml->{'Parent'}->{'ChildElement'};

This has the advantage of being consistent if you encounter an XML tag with a hyphen, for instance. $xml->{'Parent-Node'}




回答3:


To get rid of the annoying warning you have several options:

1) Clear and readable: Define a stub-class somewhere in your project:

class myXmlPhpRepresentation {
   /** @var string */
   public $childElement;
   /** @var number */
   public $anotherXmlProperty;
}

You don't need to require the class, just have it in the project folder for IDE to index. Then just use PhpDoc to "mix" that class with SimpleXMLElement:

/** @var myXmlPhpRepresentation|SimpleXMLElement $xml */
$xml = simplexml_load_string($string);
echo $xml->childElement;

It's a good practice: you will have a properly defined and readable php representation of your xml, which will help with code autocompletion and remove the warning.

2) Suppress the very warning on the code line:

 /** @noinspection PhpUndefinedFieldInspection */
 echo $xml->childElement;

Alt+Enter on the warning -> Inspection ... options -> Suppress for statement

Not a very clean way, but the inspection will help you on the rest of the project.

3) Disable the whole inspection.

I guess it's a bad practice, but some inspections may be opinionated.




回答4:


Since PHPStorm is not compiling your Code "on the Fly" it doesnt know what's in "$string" if you loaded that String from a extern source.

You can look into the Plugin Repository if there's something that helps you out:

http://plugins.intellij.net/?webide



来源:https://stackoverflow.com/questions/9666019/phpstorm-and-magic-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!