syntatis/phpstan-psr-11
最新稳定版本:v0.1.0
Composer 安装命令:
composer require --dev syntatis/phpstan-psr-11
包简介
PHPStan dynamic return type extension for PSR-11
README 文档
README
PHPStan rules to resolve value type from PSR-11
ContainerInterface.
The get method in the ContainerInterface interface return type is mixed. This is not really useful for static analysis tools like PHPStan. Since it cannot infer the type of the service. You will need to add a PHPDoc comment to the variable to help PHPStan infer the type or check the type of the service at runtime with is_a or instanceof or use assertion to narrow down the type.
For example:
use Bar\Service; use Psr\Container\ContainerInterface; class Foo { public function __construct(ContainerInterface $container) { $service = $container->get(Service::class); // PHPStan cannot infer the type of `$service`. // Check the type of the service at runtime. if ($service instanceof Service) { } } }
This package adds a return type to the get method, based on the requested service. You do not need to add PHPDoc comments or check the type of the service at runtime.
For example:
use Bar\Service; use Psr\Container\ContainerInterface; class Foo { public function __construct(ContainerInterface $container) { $service = $container->get(Service::class); // PHPStan inferred the type `$service` as Bar\Service. } }
Installation
Install the package with Composer:
composer require --dev syntatis/phpstan-psr-11
Include extension.neon in your project's PHPStan config:
includes:
- vendor/syntatis/phpstan-psr-11/extension.neon
Or, use phpstan/extension-installer.
Configuration
By default, the rule will check Psr\Container\ContainerInterface. If you've scoped or prefixed the interface, you can configure the rule to check the interface by providing the interface name in the configuration. For example:
parameters: syntatis: psr-11: 'Acme\Psr\Container\ContainerInterface'
Credits
This packaged is based on phpstan-container-extension by Phil Nelson.
统计信息
- 总下载量: 2.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-27