Skip to content

fix: recognize custom expectations provided by methods class reflection extensions - #24

Merged
MrPunyapal merged 1 commit into
pestphp:5.xfrom
sebj54:fix/custom-expectation-on-class-string
Oct 28, 2026
Merged

MrPunyapal merged 1 commit into
pestphp:5.xfrom
sebj54:fix/custom-expectation-on-class-string

Conversation

@sebj54

@sebj54 sebj54 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Since #17, isKnownExpectationMethod() only recognizes the native and @mixin methods of Pest\Expectation. A custom expectation that a project declares to PHPStan through a MethodsClassReflectionExtension (the usual way to type expect()->extend() calls) is therefore treated as a higher-order method call on the expectation value.

When that value is a class-string, the call crashes the analysis:

expect(Post::class)->toBeCustom();
Internal error: Internal error. while analysing file …
  src/Type/Pest/HigherOrderExpectationTypeExtension.php(225): PHPStan\Type\StringType->getMethod('toBeCustom', …)

hasMethod() on a class-string is not no(), so resolveMethodReturnType() calls getMethod() on a StringType, which throws something other than MissingMethodFromReflectionException.

This PR uses ClassReflection::hasMethod() instead. It covers native methods, @mixin methods and methods provided by extensions, so the existing higher-order behavior from #17 is unchanged.

The new CustomExpectationMethodCallTest registers such an extension and fails with the internal error above without the fix. composer lint, composer test:types and the whole suite pass with it.

@MrPunyapal
MrPunyapal merged commit ec3cdfc into pestphp:5.x Oct 28, 2026
10 checks passed
@MrPunyapal

Copy link
Copy Markdown
Member

Thanks

@sebj54
sebj54 deleted the fix/custom-expectation-on-class-string branch October 29, 2026 10:09
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.

2 participants