PhpStorm not typehinting class method return types in trait

1.2k Views Asked by At

Suppose i have a set of classes

class A {

    use Dummy;

    function getB() : B { ... }

}

class B {

    function foo() { ... }

}



trait Dummy {

    function works() {
        if ($this instanceof A)
            $this -> getB() -> foo();   // Typehinting works
    }

    function doesntWork() {
        $this -> getB() -> foo(); // Method foo not found in...
    }

}

How do I get PhpStorm to get typehinting work as supposed in method doesntWork? I really get annoyed by this because lots of my work relies on using main class methods in traits like this and I get lots of distracting yellow color in PhpStorm...

2

There are 2 best solutions below

1
LazyOne On BEST ANSWER

You cannot do much right now -- it's an IDE issue.

https://youtrack.jetbrains.com/issue/WI-35952 -- watch this ticket (star/vote/comment) to get notified on any progress.

Related: https://youtrack.jetbrains.com/issue/WI-39004

1
Арсен Гоян On

It turns out there's a workaround. In that case,

/**
* @method \Namespace\Name\B getB
*/
trait Dummy { ... }

A bit ugly but works