PHPStan extension that teaches static analysis the real return type of Aicrion\Container\Container::make() and ::get() calls, so tools like PHPStan can catch type errors in code that resolves services from the container.
Container::make() and Container::get() are declared with a mixed return type, since the container can resolve any registered service id at runtime. Without this extension, PHPStan sees:
$logger = $container->make(LoggerInterface::class);
$logger->nonExistentMethod(); // PHPStan: no error, because $logger is `mixed`With this extension installed, PHPStan instead infers the real class:
$logger = $container->make(LoggerInterface::class);
// PHPStan now knows $logger is LoggerInterface
$logger->nonExistentMethod(); // PHPStan: Call to undefined method LoggerInterface::nonExistentMethod()composer require --dev aicrion/container-phpstanIf you use phpstan/extension-installer (recommended), the extension is activated automatically. Otherwise, add it manually to your phpstan.neon:
includes:
- vendor/aicrion/container-phpstan/extension.neon// Class constant -- fully supported
$service = $container->make(PaymentService::class);
// String literal -- fully supported
$service = $container->make('App\PaymentService');
// Dynamic string (variable, concatenation, etc.) -- falls back to the
// declared `mixed` return type, since PHPStan cannot know the value
// at analysis time.
$id = $this->resolveServiceId();
$service = $container->make($id);- PHP 8.5+
aicrion/container^1.0phpstan/phpstan^2.0
MIT