Skip to content

Commit f6d969a

Browse files
committed
ContinueBreakHandler
1 parent 034622c commit f6d969a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser\Generator\StmtHandler;
4+
5+
use Generator;
6+
use PhpParser\Node\Stmt;
7+
use PhpParser\Node\Stmt\Break_;
8+
use PhpParser\Node\Stmt\Continue_;
9+
use PHPStan\Analyser\ExpressionContext;
10+
use PHPStan\Analyser\Generator\ExprAnalysisRequest;
11+
use PHPStan\Analyser\Generator\GeneratorScope;
12+
use PHPStan\Analyser\Generator\InternalStatementExitPoint;
13+
use PHPStan\Analyser\Generator\StmtAnalysisResult;
14+
use PHPStan\Analyser\Generator\StmtHandler;
15+
use PHPStan\Analyser\StatementContext;
16+
use PHPStan\DependencyInjection\AutowiredService;
17+
18+
/**
19+
* @implements StmtHandler<Continue_|Break_>
20+
*/
21+
#[AutowiredService]
22+
final class ContinueBreakHandler implements StmtHandler
23+
{
24+
25+
public function supports(Stmt $stmt): bool
26+
{
27+
return $stmt instanceof Continue_ || $stmt instanceof Break_;
28+
}
29+
30+
public function analyseStmt(Stmt $stmt, GeneratorScope $scope, StatementContext $context, ?callable $alternativeNodeCallback): Generator
31+
{
32+
if ($stmt->num !== null) {
33+
$result = yield new ExprAnalysisRequest($stmt, $stmt->num, $scope, ExpressionContext::createDeep(), $alternativeNodeCallback);
34+
35+
return new StmtAnalysisResult(
36+
$result->scope,
37+
hasYield: $result->hasYield,
38+
isAlwaysTerminating: true,
39+
exitPoints: [new InternalStatementExitPoint($stmt, $result->scope)],
40+
throwPoints: $result->throwPoints,
41+
impurePoints: $result->impurePoints,
42+
);
43+
}
44+
45+
return new StmtAnalysisResult(
46+
$scope,
47+
hasYield: false,
48+
isAlwaysTerminating: true,
49+
exitPoints: [new InternalStatementExitPoint($stmt, $scope)],
50+
throwPoints: [],
51+
impurePoints: [],
52+
);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)