Skip to content

Commit 48ee3bf

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

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
38793879
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
38803880
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
38813881
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
3882+
$throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr);
38823883
} else {
38833884
$this->callNodeCallback($nodeCallback, $expr->name, $scope, $storage);
38843885
}

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,9 @@ public function testBug9146NonStrict(): void
681681
]);
682682
}
683683

684+
public function testBug13569(): void
685+
{
686+
$this->analyse([__DIR__ . '/data/bug-13569.php'], []);
687+
}
688+
684689
}
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)