Skip to content

fix: resolve class string method detection - #25

Merged
MrPunyapal merged 1 commit into
pestphp:5.xfrom
owenvoke:bugfix/maybe-method-class-string
Oct 30, 2026
Merged

MrPunyapal merged 1 commit into
pestphp:5.xfrom
owenvoke:bugfix/maybe-method-class-string

Conversation

@owenvoke

Copy link
Copy Markdown
Contributor

#24 fixed the internal error for custom expectations declared through a MethodsClassReflectionExtension. The same crash still happens for any method Pest\Expectation doesn't know about when the expectation value is a class-string. That includes custom expectations registered with expect()->extend() in tests/Pest.php, which PHPStan never executes:

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

The method is unknown to the plugin, so resolveExpectationMethodCall() treats it as a higher-order call on the value. StringType::hasMethod() returns "maybe" for class-strings (to allow static calls), so the ->no() guard in resolveMethodReturnType() lets the lookup continue. getMethod() on a StringType then throws ShouldNotHappenException rather than MissingMethodFromReflectionException, so it isn't caught.

This PR also returns mixed when the value can't be an object (isObject()->no()), since a class-string has no instance methods to resolve. Nullable objects such as Post|null and mixed values still resolve as before.

resolvePropertyType() doesn't need the same change: every string type answers hasProperty() with a definite "no", which the existing guard already handles.

expect(Post::class)->foo() is added to the higher-order method call fixture. It fails with the internal error above without the fix, and with it reports the same undefined method error as expect('string')->foo(). composer test:lint, composer test:types and the whole suite pass.

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

Copy link
Copy Markdown
Member

Thanks

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