type hinting for inspections? (preventing bogus 'not found' errors)

Hey folks, I'm beating my head against an annoying bogus inspection error. I'm hoping someone has already found a solution.

In a nutshell I have an object that came from an array. I know it's an instance of class Foo, and so I can call method bar() on it... but the inspector doesn't know what it is, so it tells me "Method 'bar' not found in class'. (There are similar issues with member variables and such.)

Here's an example you can paste into a flle in phpStorm:

<?php

class A {     public static function fetch() {         $class = get_called_class();         return [new $class(), new $class(), new $class()];     }     public function question() {         return 42;     } } class B extends A {     public function yada() {         return 'no soup for you!';     } } $a = new A(); $a->question(); //this is ok, it knows $a isa A

$a->yada(); //this flags an error, 'Method yada not found in class A' - exactly what I expect it to do

$as = A::fetch(); foreach ($as as $a) {     $a->question(); //this flags an error, 'Mehtod question not found in class' }  $bs = B::fetch(); foreach ($bs as $b) {     $b->question(); //this flags an error, 'Mehtod question not found in class'     $b->yada(); //this flags an error, 'Mehtod yada not found in class' }


Failing any clean way to tell it, "$b isa B", then I guess if it doesn't know what class a variable is, is there a way to get it not to complain that it can't find a method/variable in it?
3 comments
Comment actions Permalink

wow, the code snippet in there got really trashed when I posted it, let's try again without the useless formatting macro:

<?php

class A {
    public static function fetch() {
        $class = get_called_class();
        return [new $class(), new $class(), new $class()];
    }
    public function question() {
        return 42;
    }
}

class B extends A {
    public function yada() {
        return 'no soup for you!';
    }
}

$a = new A();
$a->question(); //this is ok, it knows $a isa A
$a->yada(); //this flags an error, 'Method yada not found in class A' - exactly what I expect it to do

$as = A::fetch();
foreach ($as as $a) {
    $a->question(); //this flags an error, 'Mehtod question not found in class'
}

$bs = B::fetch();
foreach ($bs as $b) {
    $b->question(); //this flags an error, 'Mehtod question not found in class'
    $b->yada(); //this flags an error, 'Mehtod yada not found in class'
}
0
Comment actions Permalink

Hi Chris,

Add PHPDoc comment with type hint information:

 
/** @var A[] $as */
$as = A::fetch();

...

 
/** @var B[] $as */
$bs = B::fetch();


In v6 you possibly be able to use  @return static[]  in PHPDoc comment for the fetch() method (it does not work right now, but hopefully it will be implemented -- just do not count on this too hard as I do not think that it follows current PHPDoc standards).

class A {
    /**
     * @return static[]
     */
    public static function fetch() {
...
0
Comment actions Permalink

That inline @var PHPDoc works like a charm! And now lots of my files will go from Yellow to Green. :) Thanks!!

It would be very nice if static was added to the PHPDoc type list... but I won't hold my breath on that one.

0

Please sign in to leave a comment.