PHPackages                             dpanta94/phpstan-containers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. dpanta94/phpstan-containers

ActivePhpstan-extension[PSR &amp; Standards](/categories/psr-standards)

dpanta94/phpstan-containers
===========================

PHPStan extensions for Containers

0.2.0(3w ago)026.9k↓88.8%11MITPHPPHP ^7.4 || ^8.0CI passing

Since Dec 11Pushed 3w agoCompare

[ Source](https://github.com/dpanta94/phpstan-containers)[ Packagist](https://packagist.org/packages/dpanta94/phpstan-containers)[ RSS](/packages/dpanta94-phpstan-containers/feed)WikiDiscussions main Synced yesterday

READMEChangelog (2)Dependencies (12)Versions (3)Used By (1)

PHPStan Container Extensions
============================

[](#phpstan-container-extensions)

[![PHPStan](https://camo.githubusercontent.com/c961f520ec74aa0d9e60565e511aebb1c4930b5a0ea635e9be3e99bf562be287/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d323339393232)](https://github.com/phpstan/phpstan)

PHPStan extensions for dependency injection containers. Provides dynamic return type resolution so PHPStan understands that `$container->get(Foo::class)` returns `Foo`.

Supported Containers
--------------------

[](#supported-containers)

- [StellarWP Container Contract](https://github.com/stellarwp/container-contract) (`StellarWP\ContainerContract\ContainerInterface`)
- [PSR-11 Container](https://www.php-fig.org/psr/psr-11/) (`Psr\Container\ContainerInterface`)
- Any container really as long as you pass its fully qualified class name to the extension as an argument.

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- PHPStan 2.0 or higher

Installation
------------

[](#installation)

```
composer require --dev dpanta94/phpstan-containers
```

If you use [phpstan/extension-installer](https://github.com/phpstan/extension-installer), you're all set!

### Manual Installation

[](#manual-installation)

If you don't use the extension installer, add the extension to your `phpstan.neon`:

```
includes:
    - vendor/dpanta94/phpstan-containers/extension.neon
```

Usage
-----

[](#usage)

Once installed, PHPStan will automatically understand container return types:

```
use Psr\Container\ContainerInterface;

class MyService {
    public function __construct(private ContainerInterface $container) {}

    public function doSomething(): void {
        // PHPStan knows $logger is an instance of Logger
        $logger = $this->container->get(Logger::class);
        $logger->info('Hello world');

        // PHPStan knows $mailer is an instance of MailerInterface
        $mailer = $this->container->get(MailerInterface::class);
        $mailer->send($message);
    }
}
```

The extension resolves types when:

- The argument to `get()` is a class-string constant (e.g., `Foo::class`)
- The class or interface exists in the codebase

When using string service IDs (e.g., `$container->get('mailer')`), the extension falls back to the default `mixed` return type.

Prefixed containers (Strauss / Mozart)
--------------------------------------

[](#prefixed-containers-strauss--mozart)

PHPStan keys dynamic return type extensions by the class returned from `getClass()` and only consults them when the value's type has that class in its ancestry. If you ship your dependencies through a namespace prefixer such as [Strauss](https://github.com/BrianHenryIE/strauss) or Mozart, your container interface is rewritten (e.g. `StellarWP\ContainerContract\ContainerInterface` becomes `Acme\Vendor\StellarWP\ContainerContract\ContainerInterface`). The bundled StellarWP and PSR extensions can no longer match it, because the prefixed copy shares no ancestor with the original interface.

Register `ConfigurableContainerDynamicReturnTypeExtension` once per prefixed container interface in your `phpstan.neon`:

```
services:
    -
        class: DPanta\PHPStan\Containers\ConfigurableContainerDynamicReturnTypeExtension
        arguments:
            containerClass: Acme\Vendor\StellarWP\ContainerContract\ContainerInterface
        tags:
            - phpstan.broker.dynamicMethodReturnTypeExtension
```

The bundled defaults remain registered, so unprefixed containers keep working with zero configuration.

License
-------

[](#license)

MIT

Credits
-------

[](#credits)

This package is inspired by [Phil Nelson](https://github.com/phil-nelson)'s [phpstan-container-extension](https://packagist.org/packages/phil-nelson/phpstan-container-extension).

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance95

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 Bus Factor1

Top contributor holds 93.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~179 days

Total

2

Last Release

24d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70a2847a265444714b48c64eceb3ca742baa3a56757ce65b18bd7bbbbf910312?d=identicon)[dpanta94](/maintainers/dpanta94)

---

Top Contributors

[![dpanta94](https://avatars.githubusercontent.com/u/29694484?v=4)](https://github.com/dpanta94 "dpanta94 (15 commits)")[![estevao90](https://avatars.githubusercontent.com/u/18039589?v=4)](https://github.com/estevao90 "estevao90 (1 commits)")

---

Tags

containerPSR-11PHPStanstatic analysisdependency-injectioncode analysiscode analyze

###  Code Quality

TestsPHPUnit

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dpanta94-phpstan-containers/health.svg)

```
[![Health](https://phpackages.com/badges/dpanta94-phpstan-containers/health.svg)](https://phpackages.com/packages/dpanta94-phpstan-containers)
```

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.5k55.4M8.4k](/packages/larastan-larastan)[php-di/php-di

The dependency injection container for humans

2.9k55.5M1.2k](/packages/php-di-php-di)[shipmonk/dead-code-detector

Dead code detector to find unused PHP code via PHPStan extension. Can automatically remove dead PHP code. Supports libraries like Symfony, Doctrine, PHPUnit etc. Detects dead cycles. Can detect dead code that is tested.

4853.5M91](/packages/shipmonk-dead-code-detector)[calebdw/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

15118.7k4](/packages/calebdw-larastan)[slince/di

A flexible dependency injection container

20272.1k6](/packages/slince-di)[phpwatch/simple-container

A fast and minimal PSR-11 compatible Dependency Injection Container with array-syntax and without auto-wiring

1811.1k2](/packages/phpwatch-simple-container)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
