简体   繁体   中英

How to write generics for factories using psalm and phpstan

I'm trying out phpstan and psalm for php and I would like to write a class that can take different type of objects and return the right one based on the factory to call.

What I'm trying to achieve is that if I pass an object of type A to the Transformer, the compiler knows that a SuperA will be returned.

While I can go without errors in psalm (though I still get SuperA|SuperB instead of the right object), I got an error on what I'm passing in phpstan.

https://phpstan.org/r/4fce6f46-7aea-4f73-8259-df895910f064

https://psalm.dev/r/352e64ea95

Is there a way to do it?

So you want to get SuperA based on A and SuperB based on B.

I'd connect A+SuperA and B+SuperB together like this: https://phpstan.org/r/28e4e6ec-887b-4735-9b34-c034b4fa04ec

/**
 * @template TSuper of Super
 */
interface Common
{
}

/**
 * @implements Common<SuperA>
 */ 
class A implements Common
{
}

/**
 * @implements Common<SuperB>
 */ 
class B implements Common
{
}

interface Super
{
}

class SuperA implements Super
{
    public function callA(): void{}
}

class SuperB implements Super
{
    public function callB(): void{}
}

The factory then needs to have this signature:

/**
 * @template T of Super
 * @param Common<T> $obj
 * @return T
 */
public function transform($obj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM