7

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!

Sam Williams
  • 743
  • 1
  • 6
  • 15
  • If you were to annotate your $xml variable as "var SimpleXMLElement", then I would expect the IDE to then be able to recognize its type and allow for autocompletion of the methods/properties of a SimpleXMLElement object. However, this further assumes that the IDE itself has such a SimpleXMLElement class specified in its "PHP internals" code. Eclipse PDT (Indigo) does *not* seem to have it :-( – ashnazg Mar 14 '12 at 16:35

4 Answers4

10

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
10

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'}

Sam Williams
  • 743
  • 1
  • 6
  • 15
  • It would be a nice feature to allow marking certain classes as containing dynamic properties (`SimpleXMLElement`, `stdClass`, `Zend_View_Interface`, etc) which would cause PhpStorm to ignore properties it didn't know about. – David Harkness Oct 02 '14 at 22:09
  • 5
    Let me get this straight - you're writing less-readable code so that you can avoid squiggly lines in your IDE, even though functionally it doesn't create any problems? – Oddman Oct 28 '15 at 19:05
  • The answer is completely wrong and encourages bad practice: I agree with @Oddman that you just show errors prone code to suppress IDE warning. You can suppress a warning with ```/** @noinspection PhpUndefinedFieldInspection */``` annotation. – Dmitry Nevzorov Dec 13 '18 at 09:14
2

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.

Dmitry Nevzorov
  • 595
  • 8
  • 16
0

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

riyuk
  • 174
  • 1
  • 6