PHPackages                             imsuperlative/phpstan-pest - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. imsuperlative/phpstan-pest

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

imsuperlative/phpstan-pest
==========================

PHPStan extension that types $this in Pest PHP test closures

v0.5.0(5mo ago)06.2k↑127.3%1MITPHPPHP ^8.4

Since Feb 21Pushed 5mo agoCompare

[ Source](https://github.com/ImSuperlative/phpstan-pest)[ Packagist](https://packagist.org/packages/imsuperlative/phpstan-pest)[ RSS](/packages/imsuperlative-phpstan-pest/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (5)Versions (6)Used By (1)

phpstan-pest
============

[](#phpstan-pest)

PHPStan extension that provides typed `$this` in Pest PHP test closures.

Solves the "undefined property" errors PHPStan reports when you assign dynamic properties to `$this` in `beforeEach` and use them in your tests.

> **Note:** This extension uses some [unstable PHPStan APIs](#phpstan-api-compatibility) and may require updates on minor PHPStan releases.

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

[](#requirements)

- PHP 8.4+
- PHPStan 2.0+

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

[](#installation)

```
composer require --dev imsuperlative/phpstan-pest
```

If you have `phpstan/extension-installer`, the extension is registered automatically. Otherwise, add it to your `phpstan.neon`:

```
includes:
    - vendor/imsuperlative/phpstan-pest/extension.neon
```

Configuration
-------------

[](#configuration)

```
parameters:
    PhpstanPest:
        testCaseClass: PHPUnit\Framework\TestCase  # Base test case class
        parseAssignments: true                     # $this->prop = expr inference
        parsePestPropertyTags: false               # @pest-property tags
        parsePhpDocProperties: false               # @property PHPDoc tags
        parseUses: true                            # Resolve uses()/pest()->extend() traits
        parseParentUses: true                      # Walk up for parent Pest.php files
        expectationPropertyAccess: true            # Higher-order property access on expect()
        expectationSequenceTypes: false            # Typed closures in ->sequence()
        expectationScopedTypes: false              # Typed closures in ->scoped()
        simplifyToBe: true                         # ->toBe(true/false) → ->toBeTrue/False()
        simplifyToBeNull: true                     # ->toBe(null) → ->toBeNull()
        simplifyToBeEmpty: true                    # ->toBe('')/->toBe([]) → ->toBeEmpty()
        simplifyToHaveCount: true                  # ->toHaveCount(0) → ->toBeEmpty()
        simplifyToHaveLength: true                 # ->toHaveLength(0) → ->toBeEmpty()
```

If your project extends the base TestCase (e.g. in Laravel with Testbench), point `testCaseClass` to your own class:

```
parameters:
    PhpstanPest:
        testCaseClass: Tests\TestCase
```

Features
--------

[](#features)

### Typed dynamic properties

[](#typed-dynamic-properties)

Infers types from `$this->prop = expr` assignments in `beforeEach` using PHPStan's own type resolver:

```
beforeEach(function () {
    $this->user = User::factory()->create();
});

it('has a name', function () {
    // PHPStan knows $this->user is User
    expect($this->user->name)->toBeString();
});
```

Alternatively, declare types via `@pest-property` or `@property` PHPDoc tags.

[Full documentation](docs/features/typed-dynamic-properties.md)

### Uses &amp; trait resolution

[](#uses--trait-resolution)

Resolves trait methods from `uses()` and `pest()->extend()` calls, including parent `Pest.php` files with `->in()` scoping:

```
// tests/Pest.php
pest()->extends(HasGreeting::class)->in('Inherited');

// tests/Inherited/FooTest.php
it('can call trait methods', function () {
    expect($this->greet('world'))->toBe('Hello, world!');
});
```

[Full documentation](docs/features/uses-trait-resolution.md)

### Expectations

[](#expectations)

Higher-order method proxying, property access on `expect()`, union type narrowing, and custom expectations:

```
it('proxies and accesses properties', function () {
    expect($user)
        ->name->toBe('Test');

    expect(collect([1, 2, 3]))->first()->toBe(1);
});

// Union types are automatically narrowed to members that have the accessed property/method
it('narrows union types', function () {
    /** @var Order|Order[]|null $order */
    $order = Order::factory()->create();

    expect($order)
        ->id->toBeInt()          // narrows to Order (only Order has ->id)
        ->status->toBeString();
});
```

[Full documentation](docs/features/expectations.md)

Rules
-----

[](#rules)

### Assertion simplification

[](#assertion-simplification)

Suggests simpler Pest assertion methods when a more expressive alternative exists:

BeforeAfter`->toBe(true)``->toBeTrue()``->toBe(false)``->toBeFalse()``->toBe(null)``->toBeNull()``->toBe('')` / `->toBe([])``->toBeEmpty()``->toHaveCount(0)``->toBeEmpty()``->toHaveLength(0)``->toBeEmpty()`[Full documentation](docs/rules/assertion-simplification.md)

Supported Pest functions
------------------------

[](#supported-pest-functions)

`it`, `test`, `describe`, `beforeEach`, `afterEach` (both short and `Pest\` namespaced forms).

PHPStan API Compatibility
-------------------------

[](#phpstan-api-compatibility)

This extension relies on PHPStan APIs not covered by the backward compatibility promise. These are baselined and may require updates on minor PHPStan releases.

APIWhy`WrappedExtendedPropertyReflection`Wraps `PropertyReflection` into `ExtendedPropertyReflection` for property access on custom types`UnresolvedPropertyPrototypeReflection`Required by `ObjectType::getUnresolvedPropertyPrototype()``UnresolvedMethodPrototypeReflection`Required for trait method resolution on custom types`ExtendedMethodReflection`Required for exposing trait methods as public`PhpDocStringResolver::resolve()`Parses PHPDoc blocks for `@pest-property` and `@property` tagsLicense
-------

[](#license)

MIT

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance73

Regular maintenance activity

Popularity25

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

Every ~4 days

Total

5

Last Release

150d ago

PHP version history (2 changes)v0.1.0PHP ^8.2

v0.3.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22404758?v=4)[Ruben](/maintainers/ImSuperlative)[@ImSuperlative](https://github.com/ImSuperlative)

---

Top Contributors

[![ImSuperlative](https://avatars.githubusercontent.com/u/22404758?v=4)](https://github.com/ImSuperlative "ImSuperlative (13 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/imsuperlative-phpstan-pest/health.svg)

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

###  Alternatives

[nunomaduro/mock-final-classes

Allows mocking of final methods and classes in PHP.

1141.0M31](/packages/nunomaduro-mock-final-classes)[structuraphp/structura

Architectural testing for PHP

141.6k5](/packages/structuraphp-structura)

PHPackages © 2026

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