Fix #13569: Catching undefined constant causes dead catch false positive#5105
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix #13569: Catching undefined constant causes dead catch false positive#5105phpstan-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 dynamic class constant fetch (e.g. Foo::{$name})
- Dynamic class constant fetches can throw \Error at runtime if the constant doesn't exist
- New regression test in tests/PHPStan/Rules/Exceptions/data/bug-13569.php
Closes phpstan/phpstan#13569
VincentLanglet
requested changes
Feb 28, 2026
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
I already close it because of #5096 (comment)
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 reporting a false positive "Dead catch - Error is never thrown in the try block" because no throw point was registered for dynamic class constant fetch expressions.Changes
Expr\ClassConstFetchwith a dynamic name ($expr->name instanceof Expr) insrc/Analyser/NodeScopeResolver.phptests/PHPStan/Rules/Exceptions/data/bug-13569.phpand test method intests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.phpRoot cause
In
NodeScopeResolver::processExprNode(), when handlingExpr\ClassConstFetch, the code correctly processed sub-expressions (the class and name expressions) but did not add an implicit throw point for the fetch itself when the name was dynamic. Since the constant name is determined at runtime, accessing a non-existent constant throws\Error. Without this throw point, the dead catch detection rule incorrectly concluded that\Errorcould never be thrown in the try block.Test
The regression test uses an enum with a
tryFromNamemethod that usesReactionType::{$name}inside a try/catch block catching\Error. The test verifies no dead catch error is reported.Fixes phpstan/phpstan#13569