laminas / laminas-servicemanager

Factory-Driven Dependency Injection Container
https://docs.laminas.dev/laminas-servicemanager/
BSD 3-Clause "New" or "Revised" License
154 stars 57 forks source link

Psalm types do not allow the use of static factories #239

Open gsteel opened 2 months ago

gsteel commented 2 months ago

Bug Report

Q A
Version(s) 4.x

Summary

I often create what I call "static factories" along the lines of

class SomeFactory {
    public function __construct(private string $key) {
    }

    public static function __callStatic(string $method, array $arguments): Whatever
    {
        $container = $arguments[0] ?? null;
        assert($container instanceof ContainerInterface);

        return (new self($method))->__invoke($container);
    }

    public function __invoke(ContainerInterface $container): Whatever
    {
        $config = $this->key;

        return new Whatever(
            $container->get(SomeDependency::class)
            $config,
        );
    }
}

and then refer to these in config as:

return [
    'factories' => [
        Whatever::class => [SomeFactory::class, 'some-key'],
    ],
];

Current behavior

Adding callables like this is causing SA issues in projects

Expected behavior

No SA issues listing regular callables

Can we simply add callable to the psalm-type unions on ServiceManager for factories, delegators etc?