PHPackages                             mtorromeo/phpstan-yield - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mtorromeo/phpstan-yield

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mtorromeo/phpstan-yield
=======================

PHPStan extension to support @yield

v0.1.0(3mo ago)1446MITPHPCI passing

Since Mar 23Pushed 3mo agoCompare

[ Source](https://github.com/mtorromeo/phpstan-yield)[ Packagist](https://packagist.org/packages/mtorromeo/phpstan-yield)[ RSS](/packages/mtorromeo-phpstan-yield/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

phpstan-yield
=============

[](#phpstan-yield)

[![Latest Version](https://camo.githubusercontent.com/45e2966f49c80f52b718a99e02427e642d90b0a48dc5b17f3814915caad27c2c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d746f72726f6d656f2f7068707374616e2d7969656c64)](https://packagist.org/packages/mtorromeo/phpstan-yield)[![Open Issues](https://camo.githubusercontent.com/8ec80a8ad31d09ea014f61b45210f57093eb1df0e81d7e642123861a69c8de60/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6d746f72726f6d656f2f7068707374616e2d7969656c64)](https://github.com/mtorromeo/phpstan-yield/issues)[![Coverage](https://camo.githubusercontent.com/f6859e31da839a8667cf9a3a2ccaf110f80f5533bbc119b3280b181de619c227/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6d746f72726f6d656f2f7068707374616e2d7969656c64)](https://codecov.io/gh/mtorromeo/phpstan-yield)[![CI](https://camo.githubusercontent.com/8465958dd4a23fa30fc4a1e1fd024e93a2138787b7d378f3eb7a11ce1a2915e6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d746f72726f6d656f2f7068707374616e2d7969656c642f63692e796d6c3f6c6162656c3d4349)](https://github.com/mtorromeo/phpstan-yield/actions/workflows/ci.yml)

A [PHPStan](https://phpstan.org/) extension that enables type inference for `yield` expressions using `@yield` annotations on classes.

Purpose
-------

[](#purpose)

PHP generators use `yield` to suspend execution and return values to the caller. However, PHPStan has no built-in way to know what type a `yield` expression resolves to on the call side — i.e., the value sent back into the generator via `Generator::send()` or resolved by an async framework.

This extension solves that by reading `@phpstan-yield`, `@psalm-yield`, or `@yield` annotations from the docblock of the class being yielded. When PHPStan encounters `yield $value`, it looks up the type annotation on the class of `$value` (and its entire class hierarchy) to infer what the `yield` expression returns.

This is particularly useful for coroutine-based async frameworks where yielding a promise-like object causes the scheduler to suspend the coroutine and eventually resume it with the resolved value.

### Example

[](#example)

```
use React\Promise\PromiseInterface;

/**
 * @return PromiseInterface
 */
function fetchUser(int $id): PromiseInterface
{
    // ...
}

$user = yield fetchUser($id);
// PHPStan now knows $user is of type User
echo $user->name;
```

Without this extension, PHPStan would infer `$user` as `mixed`. With it, the type is correctly resolved from the `@yield T` annotation in `PromiseInterface` combined with the generic argument `User`.

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

[](#requirements)

- PHP 8.1+
- PHPStan ^2.1

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

[](#installation)

To use this extension require it in composer:

```
composer require --dev mtorromeo/phpstan-yield
```

The extension registers itself automatically if you have installed phpstan/extension-installer. Otherwise, include the extension manually in your project's PHPStan configuration:

```
includes:
    - vendor/mtorromeo/phpstan-yield/extension.neon
```

Annotations
-----------

[](#annotations)

The extension recognizes three equivalent annotation tags on class docblocks:

TagDescription`@phpstan-yield `PHPStan-specific (recommended)`@psalm-yield `Psalm-compatible alias`@yield `Generic aliasThe type can be any valid PHPStan type expression, including:

- Concrete types: `@phpstan-yield string`, `@phpstan-yield User`
- Template types: `@phpstan-yield T` (resolved against generic arguments)
- Complex types: `@phpstan-yield array`

How It Works
------------

[](#how-it-works)

When PHPStan analyses a `yield $value` expression, the extension:

1. Determines the type of `$value`
2. Traverses the full class hierarchy of that type (parent classes and all interfaces) using breadth-first search
3. Looks for a `@phpstan-yield`, `@psalm-yield`, or `@yield` tag in the docblock of each class/interface
4. Resolves the annotated type in the context of the declaring class (handling namespace imports correctly)
5. If the annotation uses a template type (e.g. `@phpstan-yield T`), binds it against the actual generic arguments of `$value`

The search stops at the first class or interface in the hierarchy that provides a yield annotation. Annotations on parent classes and interfaces are inherited, so you only need to annotate the base class or interface.

Development
-----------

[](#development)

```
# Run tests
composer test

# Run static analysis
composer phpstan

# Format code
composer format
```

License
-------

[](#license)

MIT — see [LICENSE](LICENSE) for details.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance82

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity24

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf3c28133ccdc00481cfaaa7fe2436a2744c7559a510120d061edd180db1f0b9?d=identicon)[mtorromeo](/maintainers/mtorromeo)

---

Top Contributors

[![mtorromeo](https://avatars.githubusercontent.com/u/51503?v=4)](https://github.com/mtorromeo "mtorromeo (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtorromeo-phpstan-yield/health.svg)

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

###  Alternatives

[ticketswap/phpstan-error-formatter

A minimalistic error formatter for PHPStan

87670.5k53](/packages/ticketswap-phpstan-error-formatter)[slam/phpstan-extensions

Slam extension of phpstan

712.5M77](/packages/slam-phpstan-extensions)[tomasvotruba/ctor

Prefer constructor over always called setters

1524.9k19](/packages/tomasvotruba-ctor)[ergebnis/rector-rules

Provides rules for rector/rector.

10226.6k48](/packages/ergebnis-rector-rules)[shopsys/coding-standards

Coding standards definition compatible with PSR-2

20276.8k20](/packages/shopsys-coding-standards)[johnbillion/wp-compat

PHPStan extension to help verify that your PHP code is compatible with a given version of WordPress

25152.4k15](/packages/johnbillion-wp-compat)

PHPackages © 2026

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