Skip to content

Commit c587dd1

Browse files
committed
Implicit throw point is enough to no longer mark multi-catch as dead
1 parent db1d37e commit c587dd1

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,6 @@ parameters:
433433
count: 6
434434
path: src/Reflection/InitializerExprTypeResolver.php
435435

436-
-
437-
message: "#^Dead catch \\- PHPStan\\\\BetterReflection\\\\Identifier\\\\Exception\\\\InvalidIdentifierName is never thrown in the try block\\.$#"
438-
count: 1
439-
path: src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php
440-
441436
-
442437
message: "#^Creating new PHPStan\\\\Php8StubsMap is not covered by backward compatibility promise\\. The class might change in a minor PHPStan version\\.$#"
443438
count: 1
@@ -1772,11 +1767,6 @@ parameters:
17721767
count: 1
17731768
path: tests/PHPStan/Reflection/SignatureMap/Php8SignatureMapProviderTest.php
17741769

1775-
-
1776-
message: "#^Dead catch \\- OutOfBoundsException is never thrown in the try block\\.$#"
1777-
count: 1
1778-
path: tests/PHPStan/Reflection/SignatureMap/SignatureMapParserTest.php
1779-
17801770
-
17811771
message: """
17821772
#^Instantiation of deprecated class PHPStan\\\\Rules\\\\Arrays\\\\AppendedArrayItemTypeRule\\:

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,17 +1294,16 @@ private function processStmtNode(
12941294
// explicit only
12951295
if (count($matchingThrowPoints) === 0) {
12961296
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
1297-
if (!$throwPoint->isExplicit()) {
1298-
continue;
1299-
}
1300-
13011297
foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) {
13021298
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) {
13031299
continue;
13041300
}
13051301

1306-
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
13071302
$matchingCatchTypes[$catchTypeIndex] = true;
1303+
if (!$throwPoint->isExplicit()) {
1304+
continue;
1305+
}
1306+
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
13081307
}
13091308
}
13101309
}
@@ -1322,7 +1321,6 @@ private function processStmtNode(
13221321
}
13231322

13241323
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
1325-
$matchingCatchTypes[$catchTypeIndex] = true;
13261324
}
13271325
}
13281326
}

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,9 @@ public function testMagicMethods(): void
475475
]);
476476
}
477477

478+
public function testBug9406(): void
479+
{
480+
$this->analyse([__DIR__ . '/data/bug-9406.php'], []);
481+
}
482+
478483
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Bug9406;
4+
5+
function foo(): void
6+
{
7+
if ($_POST['foo'] === null) {
8+
throw new \InvalidArgumentException('Foo');
9+
}
10+
}
11+
12+
13+
function app(): void
14+
{
15+
try {
16+
foo();
17+
18+
if ($_POST['bar'] === null) {
19+
throw new \RuntimeException('Bar');
20+
}
21+
} catch (\RuntimeException|\InvalidArgumentException $e) {
22+
23+
}
24+
}
25+
26+
function app2(): void
27+
{
28+
try {
29+
foo();
30+
31+
if ($_POST['bar'] === null) {
32+
throw new \RuntimeException('Bar');
33+
}
34+
} catch (\InvalidArgumentException $e) {
35+
36+
}
37+
}

0 commit comments

Comments
 (0)