Skip to content

Commit

Permalink
yield has implicit throw point
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 23, 2021
1 parent b48fd77 commit 033aeff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public function testBug4805(): void
]);
}

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

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

namespace Bug4863;

class HelloWorld
{
/**
* @return \Generator<string>
*/
public function generate(): \Generator {
try {
yield 'test';
} catch (\Exception $exception) {
echo $exception->getMessage();
}
}
}

0 comments on commit 033aeff

Please sign in to comment.