diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 0864eb2e6a..da1627ddb2 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -1170,7 +1170,7 @@ private function processStmtNode( $matchingThrowPoints = []; $newThrowPoints = []; foreach ($throwPoints as $throwPoint) { - if (!$throwPoint->isExplicit()) { + if (!$throwPoint->isExplicit() && !$catchType->isSuperTypeOf(new ObjectType(\Throwable::class))->yes()) { continue; } $isSuperType = $catchType->isSuperTypeOf($throwPoint->getType()); diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 10512cbf3b..d708be3381 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -385,6 +385,8 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/DateTimeDynamicReturnTypes.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4821.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4838.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4879.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4820.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-4820.php b/tests/PHPStan/Analyser/data/bug-4820.php new file mode 100644 index 0000000000..9dac4f3608 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4820.php @@ -0,0 +1,40 @@ +foo) { + $this->mayThrow(); + } + + } catch (\Throwable $e) { + assertType('bool', $param->foo); + assertVariableCertainty(TrinaryLogic::createMaybe(), $result); + throw $e; + } + } + + /** + * @throws \RuntimeException + */ + private function mayThrow(): void + { + throw new \RuntimeException(); + } + +} diff --git a/tests/PHPStan/Analyser/data/bug-4879.php b/tests/PHPStan/Analyser/data/bug-4879.php new file mode 100644 index 0000000000..1c6c9536c4 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-4879.php @@ -0,0 +1,44 @@ +test(); + } catch (\Throwable $ex) { + assertVariableCertainty(TrinaryLogic::createMaybe(), $var); + } + } + + public function sayHello2(bool $bool1): void + { + try { + if ($bool1) { + throw new \Exception(); + } + + $var = 'foo'; + + $this->test(); + } catch (\Exception $ex) { + assertVariableCertainty(TrinaryLogic::createNo(), $var); + } + } + + public function test(): void + { + return; + } +}