Skip to content

fix: support higher-order method calls on expectations - #17

Open
imKenjo18 wants to merge 1 commit into
pestphp:5.xfrom
imKenjo18:higher-order-methods
Open

fix: support higher-order method calls on expectations#17
imKenjo18 wants to merge 1 commit into
pestphp:5.xfrom
imKenjo18:higher-order-methods

Conversation

@imKenjo18

Copy link
Copy Markdown

Fixes pestphp/pest#1896

Problem

When chaining method calls directly on an expectation:

$policy = new UserPolicy;

expect($policy)
    ->view($user1, $user1)->toBeTrue()
    ->update($user1, $user1)->toBeTrue()
    ->view($user1, $user2)->toBeFalse();

It causes two PHPStan errors:

  • Call to an undefined method Pest\Expectation<UserPolicy>::view()
  • Calling toBeTrue() on Expectation<UserPolicy>; assertion is impossible.

Solution

  • Added reflection support for dynamic methods on Expectation and HigherOrderExpectation.
  • Inferred the method return type (supporting arguments, enums, and nested property chains) and wrapped it in HigherOrderExpectation.
  • Passed the method return type down the expectation chain so rules like ImpossibleExpectationRule evaluate against the returned value rather than the parent object.

@imKenjo18
imKenjo18 force-pushed the higher-order-methods branch from a07b9e7 to bca9ad6 Compare September 2, 2026 21:38
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]: Higher-order expectation method calls fail with method.notFound and pest.expectation.impossible

1 participant