I'm having an issue in type-checking some methods of a trait. I'm using a sort of facade-pattern and my aim would be to inject some methods of a facade in other classes through a trait that has generic declared methods with the return type of the class that uses the trait.
This is the structure of my project:
/**
* This class has the methods I would like to inject in other classes with a trait.
* The return type of these methods should refer to the class that uses the trait.
*
* @template T
*/
class MyFacade {
/**
* @return T
*/
public function facadeMethod() { ... }
}
/**
* This the trait that injects methods in those classes that use it.
* The return type of these methods has to be the type of the facade,
* infering in it the type of the class.
*/
trait MyTrait {
/**
* @return MyFacade<static>
*/
static function traitMethod() { ... }
}
/**
* This class uses the trait.
*/
class MyClass {
use MyTrait;
}
So when i write:
$result = MyClass::traitMethod()->facadeMethod();
I would like $result to be of type MyClass, but the IDE says that the type is actually MyTrait, so it suggests me properties and methods of all the classes that use the trait, and this is useless for me.
Can someone help me with it?