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