Fix #13569: Catching undefined constant causes dead catch false positive#5096
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix #13569: Catching undefined constant causes dead catch false positive#5096phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added implicit throw point for ClassConstFetch with dynamic name ($expr->name instanceof Expr)
- Dynamic class constant fetches like Foo::{$name} can throw \Error when the constant doesn't exist
- New regression test in tests/PHPStan/Rules/Exceptions/data/bug-13569.php
Closes phpstan/phpstan#13569
| $scope = $result->getScope(); | ||
| $hasYield = $hasYield || $result->hasYield(); | ||
| $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); | ||
| $throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr); |
Contributor
There was a problem hiding this comment.
I don't think this is the right way to fix this since
- other catch won't be reported
- Something like
public static function tryFromName2(): ?self
{
$name = 'EMOJI_HEART';
try {
return ReactionType::{$name};
} catch (\Error) {
return null;
}
}
won't be reported
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
Dynamic class constant fetches like
ReactionType::{$name}can throw\Errorat runtime when the constant name doesn't exist. PHPStan was incorrectly reporting "Dead catch - Error is never thrown in the try block" for catch blocks around such expressions.Changes
src/Analyser/NodeScopeResolver.phpwhen processingClassConstFetchnodes with dynamic names ($expr->name instanceof Expr)tests/PHPStan/Rules/Exceptions/data/bug-13569.phpwith a test method inCatchWithUnthrownExceptionRuleTestRoot cause
In
NodeScopeResolver::processExprNode(), when handlingExpr\ClassConstFetch, the code correctly processed the dynamic name expression but did not register an implicit throw point for the constant fetch itself. Since accessing a non-existent class constant with a dynamic name throws\Errorat runtime, PHPStan needs to recognize this possibility. The fix addsInternalThrowPoint::createImplicit($scope, $expr)when the constant name is dynamic, matching the pattern used elsewhere (e.g., property fetches with hooks, dynamic method calls).Test
Added a regression test using a backed enum with a
tryFromNamemethod that uses dynamic class constant fetch (ReactionType::{$name}) inside a try/catch block. The test expects no errors, confirming the catch block is correctly recognized as reachable.Fixes phpstan/phpstan#13569