aicrion / container-phpstan
PHPStan extension providing accurate type inference for Aicrion\Container resolution methods.
Package info
github.com/Aicrion/container-phpstan
Type:phpstan-extension
pkg:composer/aicrion/container-phpstan
Requires
- php: >=8.5
- aicrion/container: ^1.0
- phpstan/phpstan: ^2.0
Requires (Dev)
- phpunit/phpunit: ^11.0
README
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.
Why this exists
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()
Installation
composer require --dev aicrion/container-phpstan
If 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
Supported patterns
// 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);
Requirements
- PHP 8.5+
aicrion/container^1.0phpstan/phpstan^2.0
License
MIT