Skip to content

Commit 033aeff

Browse files
committed
yield has implicit throw point
1 parent b48fd77 commit 033aeff

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,6 @@ static function () use ($expr, $rightResult): MutatingScope {
23272327
|| $expr instanceof Expr\Print_
23282328
|| $expr instanceof Expr\UnaryMinus
23292329
|| $expr instanceof Expr\UnaryPlus
2330-
|| $expr instanceof Expr\YieldFrom
23312330
) {
23322331
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep());
23332332
$throwPoints = $result->getThrowPoints();
@@ -2337,6 +2336,17 @@ static function () use ($expr, $rightResult): MutatingScope {
23372336
$hasYield = $result->hasYield();
23382337
}
23392338

2339+
$scope = $result->getScope();
2340+
} elseif ($expr instanceof Expr\YieldFrom) {
2341+
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep());
2342+
$throwPoints = $result->getThrowPoints();
2343+
$throwPoints[] = ThrowPoint::createImplicit($scope);
2344+
if ($expr instanceof Expr\YieldFrom) {
2345+
$hasYield = true;
2346+
} else {
2347+
$hasYield = $result->hasYield();
2348+
}
2349+
23402350
$scope = $result->getScope();
23412351
} elseif ($expr instanceof BooleanNot) {
23422352
$result = $this->processExprNode($expr->expr, $scope, $nodeCallback, $context->enterDeep());
@@ -2518,7 +2528,9 @@ static function () use ($finalScope, $expr): MutatingScope {
25182528
);
25192529

25202530
} elseif ($expr instanceof Expr\Yield_) {
2521-
$throwPoints = [];
2531+
$throwPoints = [
2532+
ThrowPoint::createImplicit($scope),
2533+
];
25222534
if ($expr->key !== null) {
25232535
$keyResult = $this->processExprNode($expr->key, $scope, $nodeCallback, $context->enterDeep());
25242536
$scope = $keyResult->getScope();

tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ public function testBug4805(): void
8282
]);
8383
}
8484

85+
public function testBug4863(): void
86+
{
87+
$this->analyse([__DIR__ . '/data/bug-4863.php'], []);
88+
}
89+
8590
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Bug4863;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @return \Generator<string>
9+
*/
10+
public function generate(): \Generator {
11+
try {
12+
yield 'test';
13+
} catch (\Exception $exception) {
14+
echo $exception->getMessage();
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)