Fix #11427: Wrong "dead catch" error, side effects of ArrayAccess not considered#4910
Open
phpstan-bot wants to merge 3 commits into2.1.xfrom
Open
Fix #11427: Wrong "dead catch" error, side effects of ArrayAccess not considered#4910phpstan-bot wants to merge 3 commits into2.1.xfrom
phpstan-bot wants to merge 3 commits into2.1.xfrom
Conversation
- Synthesize offsetGet throw points in ArrayDimFetch handler for ArrayAccess objects - Synthesize offsetUnset throw points in Unset_ handler for ArrayAccess objects - Synthesize offsetExists throw points in Isset_ handler for ArrayAccess objects - offsetSet was already handled; this brings the other three operations to parity - New regression test in tests/PHPStan/Rules/Exceptions/data/bug-11427.php
Automated fix attempt 1 for CI failures.
Automated fix attempt 2 for CI failures.
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 using array syntax on
ArrayAccessobjects, PHP implicitly callsoffsetGet,offsetExists,offsetUnset, oroffsetSet. These methods can throw exceptions, but onlyoffsetSet(array assignment) was generating throw points in the analysis. This caused false "dead catch" errors when catching exceptions fromisset($x[1]),$x[1]reads, andunset($x[1])on ArrayAccess implementations.Changes
src/Analyser/NodeScopeResolver.php: Added syntheticMethodCallthrow point collection for:offsetGetin theArrayDimFetchexpression handler (reading$x[1])offsetUnsetin theUnset_statement handler (forunset($x[1]))offsetExistsin theIsset_expression handler (forisset($x[1]))tests/PHPStan/Rules/Exceptions/data/bug-11427.php: Regression test with ArrayAccess class whose methods throwtests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php: AddedtestBug11427methodCLAUDE.md: Documented the ArrayAccess throw point synthesis patternRoot cause
NodeScopeResolveralready synthesized aMethodCalltooffsetSetwhen processing array assignments on ArrayAccess objects (line ~6291), which properly generated throw points for dead-catch analysis. However, the same pattern was missing for the other three ArrayAccess operations:offsetGet(array read),offsetUnset(unset), andoffsetExists(isset). Without these synthetic method calls, their potential exceptions were invisible to the dead-catch rule.Test
The regression test creates an ArrayAccess class where all four offset methods throw
\Exception, then wraps each operation in try/catch blocks. Before the fix, 3 of 4 catch blocks were incorrectly reported as dead catches. After the fix, no errors are reported.Fixes phpstan/phpstan#11427