From 033aeffa4b63428934f5000092c582c028cdc53d Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 23 Apr 2021 15:41:31 +0200 Subject: [PATCH] yield has implicit throw point --- src/Analyser/NodeScopeResolver.php | 16 ++++++++++++++-- .../CatchWithUnthrownExceptionRuleTest.php | 5 +++++ .../PHPStan/Rules/Exceptions/data/bug-4863.php | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Exceptions/data/bug-4863.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 131abbc58a..4a12206fab 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -2327,7 +2327,6 @@ static function () use ($expr, $rightResult): MutatingScope { || $expr instanceof Expr\Print_ || $expr instanceof Expr\UnaryMinus || $expr instanceof Expr\UnaryPlus - || $expr instanceof Expr\YieldFrom ) { $result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep()); $throwPoints = $result->getThrowPoints(); @@ -2337,6 +2336,17 @@ static function () use ($expr, $rightResult): MutatingScope { $hasYield = $result->hasYield(); } + $scope = $result->getScope(); + } elseif ($expr instanceof Expr\YieldFrom) { + $result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $throwPoints = $result->getThrowPoints(); + $throwPoints[] = ThrowPoint::createImplicit($scope); + if ($expr instanceof Expr\YieldFrom) { + $hasYield = true; + } else { + $hasYield = $result->hasYield(); + } + $scope = $result->getScope(); } elseif ($expr instanceof BooleanNot) { $result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep()); @@ -2518,7 +2528,9 @@ static function () use ($finalScope, $expr): MutatingScope { ); } elseif ($expr instanceof Expr\Yield_) { - $throwPoints = []; + $throwPoints = [ + ThrowPoint::createImplicit($scope), + ]; if ($expr->key !== null) { $keyResult = $this->processExprNode($expr->key, $scope, $nodeCallback, $context->enterDeep()); $scope = $keyResult->getScope(); diff --git a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php index b87445b51d..4bcd58cab2 100644 --- a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php @@ -82,4 +82,9 @@ public function testBug4805(): void ]); } + public function testBug4863(): void + { + $this->analyse([__DIR__ . '/data/bug-4863.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-4863.php b/tests/PHPStan/Rules/Exceptions/data/bug-4863.php new file mode 100644 index 0000000000..724c8a1f03 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-4863.php @@ -0,0 +1,17 @@ + + */ + public function generate(): \Generator { + try { + yield 'test'; + } catch (\Exception $exception) { + echo $exception->getMessage(); + } + } +}