First-class callable crash on Expr methods#735
Merged
staabm merged 1 commit intophpstan:2.0.xfrom Mar 10, 2026
Merged
Conversation
238be97 to
1821cee
Compare
Comment on lines
+26
to
+28
| if (PHP_VERSION_ID < 80100) { | ||
| self::markTestSkipped('Test requires PHP 8.1.'); | ||
| } |
Contributor
There was a problem hiding this comment.
#[RequiresPhp('>= 8.1')]
maybe ?
Contributor
Author
There was a problem hiding this comment.
This is not available in phpunit v9
src/Type/Doctrine/QueryBuilder/Expr/ExpressionBuilderDynamicReturnTypeExtension.php
Show resolved
Hide resolved
a971cdb to
2dbaea3
Compare
2dbaea3 to
a23654a
Compare
VincentLanglet
approved these changes
Mar 9, 2026
staabm
reviewed
Mar 9, 2026
Comment on lines
+59
to
+64
| try { | ||
| $exprValue = $expr->{$methodReflection->getName()}(...$args); | ||
| } catch (Throwable $e) { | ||
| return null; | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
this change is not covered by a test it seems
Contributor
Author
There was a problem hiding this comment.
a23654a to
6a70cb9
Compare
Contributor
|
thank you! |
When first-class callable syntax ($expr->in(...)) is used inside match arms or assigned to variables, PHPStan resolves the closure and visits the extension with a MethodCall where isFirstClassCallable() is false and getArgs() is empty. This causes the actual Expr method to be called with 0 arguments, crashing with ArgumentCountError. Catch Throwable (not just ArgumentCountError) around the reflective method invocation to also handle TypeError from typed signatures in ORM 3.x and other potential exceptions. Closes phpstan#711 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
6a70cb9 to
ef3d5a1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #711
Repro https://github.com/phpstan/phpstan-doctrine/actions/runs/22821964729/job/66196045235?pr=735#step:9:28
When first-class callable syntax ($expr->in(...)) is used inside match arms or assigned to variables, PHPStan resolves the closure and visits the extension with a MethodCall where isFirstClassCallable() is false and getArgs() is empty. This causes the actual Expr method to be called with 0 arguments, crashing with ArgumentCountError.
Fix with try/catch Throwable around the reflective method invocation for indirect cases (match arms, variable assignments). We also need to cover TypeError from ORM 3.x's typed signatures, plus any other unexpected exception from the reflective call.