Skip to content

Commit 5d66512

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix dead catch false positive for dynamic class constant fetch
- 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
1 parent 1bbe9dc commit 5d66512

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,6 +3877,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
38773877
$scope = $result->getScope();
38783878
$hasYield = $hasYield || $result->hasYield();
38793879
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
3880+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
38803881
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
38813882
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
38823883
} else {

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,11 @@ public function testBug9146(): void
667667
]);
668668
}
669669

670+
public function testBug13569(): void
671+
{
672+
$this->analyse([__DIR__ . '/data/bug-13569.php'], []);
673+
}
674+
670675
public function testBug9146NonStrict(): void
671676
{
672677
$this->analyse([__DIR__ . '/data/bug-9146-non-strict.php'], [
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13569;
6+
7+
enum ReactionType: string
8+
{
9+
case EMOJI_HEART = '❤️';
10+
11+
public static function tryFromName(string $name): ?self
12+
{
13+
try {
14+
return ReactionType::{$name};
15+
} catch (\Error) {
16+
return null;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)