syntatis/phpstan-psr-11

PHPStan dynamic return type extension for PSR-11 ContainerInterface

Installs: 420

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

Type:phpstan-extension

v1.0.0 2025-02-01 13:03 UTC

This package is auto-updated.

Last update: 2025-03-01 13:10:58 UTC


README

ci Packagist Dependency Version

PHPStan rules for 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'