Skip to content

Commit

Permalink
Implicit throw point is enough to no longer mark multi-catch as dead
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 7, 2023
1 parent db1d37e commit c587dd1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,6 @@ parameters:
count: 6
path: src/Reflection/InitializerExprTypeResolver.php

-
message: "#^Dead catch \\- PHPStan\\\\BetterReflection\\\\Identifier\\\\Exception\\\\InvalidIdentifierName is never thrown in the try block\\.$#"
count: 1
path: src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php

-
message: "#^Creating new PHPStan\\\\Php8StubsMap is not covered by backward compatibility promise\\. The class might change in a minor PHPStan version\\.$#"
count: 1
Expand Down Expand Up @@ -1772,11 +1767,6 @@ parameters:
count: 1
path: tests/PHPStan/Reflection/SignatureMap/Php8SignatureMapProviderTest.php

-
message: "#^Dead catch \\- OutOfBoundsException is never thrown in the try block\\.$#"
count: 1
path: tests/PHPStan/Reflection/SignatureMap/SignatureMapParserTest.php

-
message: """
#^Instantiation of deprecated class PHPStan\\\\Rules\\\\Arrays\\\\AppendedArrayItemTypeRule\\:
Expand Down
10 changes: 4 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,17 +1294,16 @@ private function processStmtNode(
// explicit only
if (count($matchingThrowPoints) === 0) {
foreach ($throwPoints as $throwPointIndex => $throwPoint) {
if (!$throwPoint->isExplicit()) {
continue;
}

foreach ($catchTypes as $catchTypeIndex => $catchTypeItem) {
if ($catchTypeItem->isSuperTypeOf($throwPoint->getType())->no()) {
continue;
}

$matchingThrowPoints[$throwPointIndex] = $throwPoint;
$matchingCatchTypes[$catchTypeIndex] = true;
if (!$throwPoint->isExplicit()) {
continue;
}
$matchingThrowPoints[$throwPointIndex] = $throwPoint;
}
}
}
Expand All @@ -1322,7 +1321,6 @@ private function processStmtNode(
}

$matchingThrowPoints[$throwPointIndex] = $throwPoint;
$matchingCatchTypes[$catchTypeIndex] = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,9 @@ public function testMagicMethods(): void
]);
}

public function testBug9406(): void
{
$this->analyse([__DIR__ . '/data/bug-9406.php'], []);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9406.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug9406;

function foo(): void
{
if ($_POST['foo'] === null) {
throw new \InvalidArgumentException('Foo');
}
}


function app(): void
{
try {
foo();

if ($_POST['bar'] === null) {
throw new \RuntimeException('Bar');
}
} catch (\RuntimeException|\InvalidArgumentException $e) {

}
}

function app2(): void
{
try {
foo();

if ($_POST['bar'] === null) {
throw new \RuntimeException('Bar');
}
} catch (\InvalidArgumentException $e) {

}
}

0 comments on commit c587dd1

Please sign in to comment.