lctrs/psalm-psr-container-plugin
Psalm plugin that improves static analysis for PSR-11 containers. Adds smarter type inference for container get/has calls so services and dependencies resolve to the right classes, reducing false positives and making container-heavy code safer to refactor.
composer require --dev lctrs/psalm-psr-container-plugin
psalm.xml):
<plugins>
<pluginClass class="Lctrs\PsalmPsrContainerPlugin\Plugin"/>
</plugins>
$container->get(SomeService::class). Previously returning mixed, Psalm now infers the actual return type if the service is registered (e.g., via a container config file or reflection-based metadata).@psalm-return or @psalm-param annotations in your container’s get() method or rely on auto-discovered class-levelDocBlock type hints. The plugin infers types from:
@psalm-return on factory __invoke() or get() methods@psalm-type declarations mapped to service IDs (optional but powerful)get(OldClassName::class) where no mapping exists.ContainerInterface), or custom PSR-11 implementations. Just ensure your container’s get() method is correctly typed as returning mixed or object.get($serviceId)), Psalm can’t resolve the type—this plugin only handles static service IDs (string literals or class constant strings).@psalm-type annotations to your container’s get() method, or define a psalm-container.php config file (see docs) to map service IDs → types explicitly:
// psalm-container.php
return [
'Service\EmailSenderInterface' => Service\SmtpEmailSender::class,
];
@psalm-return hints on factory classes when ambiguity remains.--debug to see why a type was inferred — look for PsalmPsrContainerPlugin entries in the output.How can I help you explore Laravel packages today?