I have a factory method whose signature is similar to this:
/**
* @param class-string $class
* @param callable? $callback
* @return static
*/
public function instance(string $class, callable? $callback);
From the first parameter, an instance is created. Rather than the instance being returned, $this is returned instead - so an optional callback can be provided and the factory invokes it, passing the new instance.
What I want to know is - given the string passed is literal, can phpstan/vscode be told that the argument passed to the callable is an instance of my specified class, so that when I refer to the object inside the callable it can make suggestions of available methods/props, and so that phpstan can not grumble about missing methods, without me also typehinting the argument of the callable?
The object created does extend a common base class, but child classes will have their own methods.
Maybe clearer with an example of a call:
$obj->factory(Foo::class, function ($foo) {
//
}
And I’m hoping to avoid repeating the explicit hint:
$obj->factory(Foo::class, function (Foo $foo) {
//
}