Skip to content

fix: resolve $this type in ->with(), dataset closures, and trait-bound test cases - #9

Merged
MrPunyapal merged 2 commits into
5.xfrom
fix/undefined-var-this-in-with
Aug 7, 2026
Merged

fix: resolve $this type in ->with(), dataset closures, and trait-bound test cases#9
MrPunyapal merged 2 commits into
5.xfrom
fix/undefined-var-this-in-with

Conversation

@MrPunyapal

@MrPunyapal MrPunyapal commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Fixes two related gaps in $this type resolution inside Pest test closures.

$this in ->with() and dataset closures

Closures passed to ->with() or used as array-dataset values now resolve $this to the bound test case, so Laravel / PHPUnit helpers can be called without PHPStan reporting an undefined variable.

pest()->extend(TestCase::class)->in('Feature');

test('locale redirect', function (string $locale): void {
    $this->get(route('set-locale', $locale))->assertStatus(302);
})->with(function (): array {
    $this->from(route('posts.index')); // $this was previously undefined

    return ['en', 'fr'];
});

Trait methods on $this

When a trait is bound alongside a test case (or alone), PHPStan now recognizes methods, properties, and constants provided by that trait, instead of only looking at the class.

pest()->extend(TestCase::class);
pest()->use(Foo::class);

test('trait methods are visible on $this', function (): void {
    expect($this->foo())->toBeTrue(); // was: Call to undefined method Tests\TestCase::foo()
});

This works for pest()->extend(...)->use(...), split pest()->extend(...) / pest()->use(...) calls, file-level uses(Trait::class), and any combination of classes and traits.

Global Pest.php bindings

Bindings declared in Pest.php without ->in('...') are now applied to every test file, matching Pest runtime behavior.

// Pest.php
pest()->extend(TestCase::class);
pest()->use(Foo::class);

Closes pestphp/pest#1815 and pestphp/pest#1827

PHPStan treats traits as non-instantiable, so intersecting a class
ObjectType with a trait ObjectType collapses to NEVER. Introduce
PestTestCaseWithTraitsType, an ObjectType subclass that delegates to
the test case class while also exposing methods/properties/constants
from bound traits.

Also extend PestConfigReader to treat uses()/pest()->... bindings
without an ->in() scope as global bindings that apply to every test
file, matching Pest runtime behavior.

Closes pestphp/pest#1815
@MrPunyapal MrPunyapal changed the title fix: undefined var $this in ->with() fix: resolve $this type in ->with(), dataset closures, and trait-bound test cases Aug 7, 2026
@MrPunyapal
MrPunyapal marked this pull request as ready for review August 7, 2026 14:58
@MrPunyapal
MrPunyapal requested a review from nunomaduro August 7, 2026 14:58
@MrPunyapal
MrPunyapal merged commit 7b224e4 into 5.x Aug 7, 2026
22 checks passed
@MrPunyapal
MrPunyapal removed the request for review from nunomaduro August 7, 2026 14:59
@MrPunyapal
MrPunyapal deleted the fix/undefined-var-this-in-with branch August 7, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Phpstan does not recognize methods from traits used in test files

1 participant