Problems with undefined fields in class with __get

I'm having some problems understand what is the best way to do this in PhpStorm 3.0.  The following class defines an ArrayClass with a getter which gets values out of an associative array.

The problem is that undefined properties show a warning in the IDE.  I followed the instructions, and changed the settings in Inspections | PHP | Undefined | Undefined field which allows me to "downgrade severity in __magic methods are present in class", and that works but I still get warning, and I don't want to get any.  For other classes which do not have a getter, I want this to be an error.  

In the following code, $testArray->id is ok, but $testArray->name is marked with a warning.  I didn't really want to add the @property tag since I don't know what the fields might be.  

I see in http://youtrack.jetbrains.net/issue/WI-1711, there is a heated discussion about this, including discussion of using the  @noinspection PhpUndefinedPropertyInspection setting, but I didn't quite understand what I was supposed to do.  I prefer this setting to be set for the class, but I've put them in both places, and it doesn't seem to make a difference.

Could someone help me with this?

I've also asked a similar question in http://devnet.jetbrains.net/thread/432438?tstart=0, but that is for stdClass, which might be a different problem.

Thanks for your help.

m



/**
* @noinspection PhpUndefinedPropertyInspection
* @property $id
*
*/
class TestArrayClass  {
    private $_array;

    public function __construct($array) {
       $this->_array = $array;
    }

    public function __get($name) {
        if (array_key_exists($name,$this->_array)) {
            return $this->_array[$name];
        }
        return false;
    }
}
/**
*  @noinspection PhpUndefinedPropertyInspection
*/
function testArray() {
    $array = array('id' => '123', 'name' => 'George');
    $testArray = new TestArrayClass($array);
    print $testArray->id . ':' . $testArray->name;
}




http://youtrack.jetbrains.net/issue/WI-1711
2 comments

Hi Marc,

This Inspection works as intended: it downgrades severity from default Warning (yellow marks) to Weak Warning (very light gray? marks). As a result, if you have no other Warnings/Errors, your whole file will be marked as Green (i.e. "No problems found").

Neither this Inspection or another cannot remove those marks completely, even if you change the base severity manually on Settings page and I do not know any other Inspection/trick that will disable it completely. I personally like it and have no issues with that as it still allows me to see what are real properties and what are virtual (in case I make a typo, since no code completion is available for such properties).

P.S.
I may be wrong here, but I think "@noinspection PhpUndefinedPropertyInspection" is no longer valid for PhpStorm v3.

0

@noinspection

works only at statement level. Press "Alt-Enter,Right" on inspection warning, you can see the only option - suppress for statement. We plan adding more suppression options (i.e. http://youtrack.jetbrains.net/issue/WI-5784)

0

Please sign in to leave a comment.