-
Notifications
You must be signed in to change notification settings - Fork 571
Expand file tree
/
Copy pathFiberNodeScopeResolver.php
More file actions
132 lines (113 loc) · 3.52 KB
/
FiberNodeScopeResolver.php
File metadata and controls
132 lines (113 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\Fiber;
use Fiber;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\ExpressionContext;
use PHPStan\Analyser\ExpressionResult;
use PHPStan\Analyser\ExpressionResultStorage;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\ShouldNotHappenException;
use function get_class;
use function get_debug_type;
use function sprintf;
#[AutowiredService(as: FiberNodeScopeResolver::class)]
final class FiberNodeScopeResolver extends NodeScopeResolver
{
/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
protected function callNodeCallback(
callable $nodeCallback,
Node $node,
MutatingScope $scope,
ExpressionResultStorage $storage,
): void
{
$fiber = new Fiber(static function () use ($node, $scope, $nodeCallback) {
$nodeCallback($node, $scope->toFiberScope());
});
$request = $fiber->start();
$this->runFiberForNodeCallback($storage, $fiber, $request);
}
/**
* @param Fiber<mixed, ExpressionResult, null, ExpressionAnalysisRequest> $fiber
*/
private function runFiberForNodeCallback(
ExpressionResultStorage $storage,
Fiber $fiber,
?ExpressionAnalysisRequest $request,
): void
{
while (!$fiber->isTerminated()) {
if ($request instanceof ExpressionAnalysisRequest) {
$result = $storage->findResult($request->expr);
if ($result !== null) {
$request = $fiber->resume($result);
continue;
}
$storage->pendingFibers[] = [
'fiber' => $fiber,
'request' => $request,
];
return;
}
throw new ShouldNotHappenException(
'Unknown fiber suspension: ' . get_debug_type($request),
);
}
if ($request !== null) {
throw new ShouldNotHappenException(
'Fiber terminated but we did not handle its request ' . get_debug_type($request),
);
}
}
protected function processPendingFibers(ExpressionResultStorage $storage): void
{
foreach ($storage->pendingFibers as $pending) {
$request = $pending['request'];
$result = $storage->findResult($request->expr);
if ($result !== null) {
throw new ShouldNotHappenException('Pending fibers at the end should be about synthetic nodes');
}
$this->processExprNode(
new Node\Stmt\Expression($request->expr),
$request->expr,
$request->scope->toMutatingScope(),
$storage,
new NoopNodeCallback(),
ExpressionContext::createTopLevel(),
);
if ($storage->findResult($request->expr) === null) {
throw new ShouldNotHappenException(sprintf('processExprNode should have stored the result of %s on line %s', get_class($request->expr), $request->expr->getStartLine()));
}
$this->processPendingFibers($storage);
// Break and restart the loop since the array may have been modified
return;
}
}
protected function processPendingFibersForRequestedExpr(ExpressionResultStorage $storage, Expr $expr, ExpressionResult $result): void
{
$restartLoop = true;
while ($restartLoop) {
$restartLoop = false;
foreach ($storage->pendingFibers as $key => $pending) {
$request = $pending['request'];
if ($request->expr !== $expr) {
continue;
}
unset($storage->pendingFibers[$key]);
$restartLoop = true;
$fiber = $pending['fiber'];
$request = $fiber->resume($result);
$this->runFiberForNodeCallback($storage, $fiber, $request);
// Break and restart the loop since the array may have been modified
break;
}
}
}
}