Fix #7719: ReflectionMethod with invokeArgs: Dead catch - exception is never thrown in the try block.#4970
Closed
phpstan-bot wants to merge 2 commits into2.1.xfrom
Closed
Fix #7719: ReflectionMethod with invokeArgs: Dead catch - exception is never thrown in the try block.#4970phpstan-bot wants to merge 2 commits into2.1.xfrom
phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
- Added ReflectionMethodInvokeMethodThrowTypeExtension to declare that ReflectionMethod::invoke() and invokeArgs() can throw any Throwable, since they execute arbitrary user code - New regression test in tests/PHPStan/Rules/Exceptions/data/bug-7719.php - The root cause was that without a DynamicMethodThrowTypeExtension, PHPStan only considered ReflectionException (from stubs) as the throw type, causing any non-ReflectionException catch to be flagged as dead
Contributor
|
duplicate of #4978 |
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.
Summary
When
ReflectionMethod::invoke()orReflectionMethod::invokeArgs()is called, the dynamically invoked method can throw any exception. PHPStan was incorrectly reporting "Dead catch" for non-ReflectionExceptioncatch blocks in try blocks containing these calls.Changes
src/Type/Php/ReflectionMethodInvokeMethodThrowTypeExtension.php— aDynamicMethodThrowTypeExtensionthat declaresReflectionMethod::invoke()andinvokeArgs()can throwThrowabletests/PHPStan/Rules/Exceptions/data/bug-7719.phptestBug7719intests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.phpRoot cause
PHPStan had
DynamicStaticMethodThrowTypeExtensionforReflectionMethod::__construct()(to narrow the throw type when class/method names are known constants), but no corresponding extension forinvoke()/invokeArgs(). Without an extension, PHPStan fell back to the@throws ReflectionExceptionannotation from PHP stubs, meaning onlyReflectionExceptionwas considered as a possible throw type. Since these methods execute arbitrary user code, they can throw anyThrowable.Test
The regression test creates a scenario where
ReflectionMethod::invokeArgs()andinvoke()are called with a dynamic method name, and aRuntimeExceptionis caught. Before the fix, PHPStan incorrectly reported these catches as dead.Fixes phpstan/phpstan#7719