Skip to content

Commit 923f1df

Browse files
committed
Rename for more clarity
1 parent f57b08f commit 923f1df

File tree

5 files changed

+53
-62
lines changed

5 files changed

+53
-62
lines changed

src/Analyser/ExpressionResultStorage.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,38 @@
44

55
use Fiber;
66
use PhpParser\Node\Expr;
7-
use PHPStan\Analyser\Fiber\ExpressionAnalysisRequest;
8-
use PHPStan\ShouldNotHappenException;
7+
use PHPStan\Analyser\Fiber\BeforeScopeForExprRequest;
98
use SplObjectStorage;
10-
use function get_class;
11-
use function sprintf;
129

1310
final class ExpressionResultStorage
1411
{
1512

1613
/** @var SplObjectStorage<Expr, Scope> */
17-
private SplObjectStorage $results;
14+
private SplObjectStorage $scopes;
1815

19-
/** @var array<array{fiber: Fiber<mixed, Scope, null, ExpressionAnalysisRequest>, request: ExpressionAnalysisRequest}> */
16+
/** @var array<array{fiber: Fiber<mixed, Scope, null, BeforeScopeForExprRequest>, request: BeforeScopeForExprRequest}> */
2017
public array $pendingFibers = [];
2118

2219
public function __construct()
2320
{
24-
$this->results = new SplObjectStorage();
21+
$this->scopes = new SplObjectStorage();
2522
}
2623

2724
public function duplicate(): self
2825
{
2926
$new = new self();
30-
$new->results->addAll($this->results);
27+
$new->scopes->addAll($this->scopes);
3128
return $new;
3229
}
3330

34-
public function storeResult(Expr $expr, Scope $scope): void
31+
public function storeBeforeScope(Expr $expr, Scope $scope): void
3532
{
36-
if (isset($this->results[$expr])) {
37-
//throw new ShouldNotHappenException(sprintf('already stored %s on line %d', get_class($expr), $expr->getStartLine()));
38-
}
39-
$this->results[$expr] = $scope;
33+
$this->scopes[$expr] = $scope;
4034
}
4135

42-
public function findResult(Expr $expr): ?Scope
36+
public function findBeforeScope(Expr $expr): ?Scope
4337
{
44-
return $this->results[$expr] ?? null;
38+
return $this->scopes[$expr] ?? null;
4539
}
4640

4741
}

src/Analyser/Fiber/ExpressionAnalysisRequest.php renamed to src/Analyser/Fiber/BeforeScopeForExprRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PhpParser\Node\Expr;
66
use PHPStan\Analyser\MutatingScope;
77

8-
final class ExpressionAnalysisRequest
8+
final class BeforeScopeForExprRequest
99
{
1010

1111
public function __construct(public readonly Expr $expr, public readonly MutatingScope $scope)

src/Analyser/Fiber/FiberNodeScopeResolver.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpParser\Node;
77
use PhpParser\Node\Expr;
88
use PHPStan\Analyser\ExpressionContext;
9-
use PHPStan\Analyser\ExpressionResult;
109
use PHPStan\Analyser\ExpressionResultStorage;
1110
use PHPStan\Analyser\MutatingScope;
1211
use PHPStan\Analyser\NodeScopeResolver;
@@ -40,19 +39,19 @@ protected function callNodeCallback(
4039
}
4140

4241
/**
43-
* @param Fiber<mixed, ExpressionResult, null, ExpressionAnalysisRequest> $fiber
42+
* @param Fiber<mixed, Scope, null, BeforeScopeForExprRequest> $fiber
4443
*/
4544
private function runFiberForNodeCallback(
4645
ExpressionResultStorage $storage,
4746
Fiber $fiber,
48-
?ExpressionAnalysisRequest $request,
47+
?BeforeScopeForExprRequest $request,
4948
): void
5049
{
5150
while (!$fiber->isTerminated()) {
52-
if ($request instanceof ExpressionAnalysisRequest) {
53-
$result = $storage->findResult($request->expr);
54-
if ($result !== null) {
55-
$request = $fiber->resume($result);
51+
if ($request instanceof BeforeScopeForExprRequest) {
52+
$beforeScope = $storage->findBeforeScope($request->expr);
53+
if ($beforeScope !== null) {
54+
$request = $fiber->resume($beforeScope);
5655
continue;
5756
}
5857

@@ -79,9 +78,9 @@ protected function processPendingFibers(ExpressionResultStorage $storage): void
7978
{
8079
foreach ($storage->pendingFibers as $pending) {
8180
$request = $pending['request'];
82-
$result = $storage->findResult($request->expr);
81+
$beforeScope = $storage->findBeforeScope($request->expr);
8382

84-
if ($result !== null) {
83+
if ($beforeScope !== null) {
8584
throw new ShouldNotHappenException('Pending fibers at the end should be about synthetic nodes');
8685
}
8786

@@ -93,8 +92,8 @@ protected function processPendingFibers(ExpressionResultStorage $storage): void
9392
new NoopNodeCallback(),
9493
ExpressionContext::createTopLevel(),
9594
);
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()));
95+
if ($storage->findBeforeScope($request->expr) === null) {
96+
throw new ShouldNotHappenException(sprintf('processExprNode should have stored the beforeScope of %s on line %s', get_class($request->expr), $request->expr->getStartLine()));
9897
}
9998
$this->processPendingFibers($storage);
10099

src/Analyser/Fiber/FiberScope.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fiber;
66
use PhpParser\Node\Expr;
7-
use PHPStan\Analyser\ExpressionResult;
87
use PHPStan\Analyser\MutatingScope;
98
use PHPStan\Analyser\Scope;
109
use PHPStan\Type\Type;
@@ -42,12 +41,12 @@ public function toMutatingScope(): MutatingScope
4241
/** @api */
4342
public function getType(Expr $node): Type
4443
{
45-
/** @var Scope $exprResult */
46-
$exprResult = Fiber::suspend(
47-
new ExpressionAnalysisRequest($node, $this),
44+
/** @var Scope $beforeScope */
45+
$beforeScope = Fiber::suspend(
46+
new BeforeScopeForExprRequest($node, $this),
4847
);
4948

50-
return $exprResult->toMutatingScope()->getType($node);
49+
return $beforeScope->toMutatingScope()->getType($node);
5150
}
5251

5352
public function getScopeType(Expr $expr): Type
@@ -63,22 +62,22 @@ public function getScopeNativeType(Expr $expr): Type
6362
/** @api */
6463
public function getNativeType(Expr $expr): Type
6564
{
66-
/** @var Scope $exprResult */
67-
$exprResult = Fiber::suspend(
68-
new ExpressionAnalysisRequest($expr, $this),
65+
/** @var Scope $beforeScope */
66+
$beforeScope = Fiber::suspend(
67+
new BeforeScopeForExprRequest($expr, $this),
6968
);
7069

71-
return $exprResult->toMutatingScope()->getNativeType($expr);
70+
return $beforeScope->toMutatingScope()->getNativeType($expr);
7271
}
7372

7473
public function getKeepVoidType(Expr $node): Type
7574
{
76-
/** @var Scope $exprResult */
77-
$exprResult = Fiber::suspend(
78-
new ExpressionAnalysisRequest($node, $this),
75+
/** @var Scope $beforeScope */
76+
$beforeScope = Fiber::suspend(
77+
new BeforeScopeForExprRequest($node, $this),
7978
);
8079

81-
return $exprResult->toMutatingScope()->getKeepVoidType($node);
80+
return $beforeScope->toMutatingScope()->getKeepVoidType($node);
8281
}
8382

8483
}

src/Analyser/NodeScopeResolver.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ public function processNodes(
342342
$this->processPendingFibers($expressionResultStorage);
343343
}
344344

345-
private function storeResult(ExpressionResultStorage $storage, Expr $expr, ExpressionResult $result): void
345+
private function storeBeforeScope(ExpressionResultStorage $storage, Expr $expr, ExpressionResult $result): void
346346
{
347-
$storage->storeResult($expr, $result->getBeforeScope());
347+
$storage->storeBeforeScope($expr, $result->getBeforeScope());
348348
$this->processPendingFibersForRequestedExpr($storage, $expr, $result->getBeforeScope());
349349
}
350350

@@ -2545,13 +2545,12 @@ public function processExprNode(
25452545
ExpressionContext $context,
25462546
): ExpressionResult
25472547
{
2548-
/*$existingExprResult = $storage->findResult($expr);
2549-
if ($existingExprResult !== null) {
2550-
if ($nodeCallback instanceof ShallowNodeCallback) {
2551-
return $existingExprResult;
2548+
$existingBeforeScope = $storage->findBeforeScope($expr);
2549+
if ($existingBeforeScope !== null) {
2550+
if (!$nodeCallback instanceof ShallowNodeCallback) {
2551+
throw new ShouldNotHappenException(sprintf('Expr %s on line %d has already been analysed', get_class($expr), $expr->getStartLine()));
25522552
}
2553-
throw new ShouldNotHappenException(sprintf('Expr %s on line %d has already been analysed', get_class($expr), $expr->getStartLine()));
2554-
}*/
2553+
}
25552554

25562555
if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) {
25572556
if ($expr instanceof FuncCall) {
@@ -2567,7 +2566,7 @@ public function processExprNode(
25672566
}
25682567

25692568
$newExprResult = $this->processExprNode($stmt, $newExpr, $scope, $storage, $nodeCallback, $context);
2570-
$this->storeResult($storage, $expr, $newExprResult);
2569+
$this->storeBeforeScope($storage, $expr, $newExprResult);
25712570
return $newExprResult;
25722571
}
25732572

@@ -2639,7 +2638,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
26392638
}
26402639

26412640
$result = new ExpressionResult($scope, $beforeScope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
2642-
$this->storeResult($storage, $expr, $result);
2641+
$this->storeBeforeScope($storage, $expr, $result);
26432642

26442643
return $result;
26452644
},
@@ -2696,7 +2695,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
26962695
$result->getThrowPoints(),
26972696
$result->getImpurePoints(),
26982697
);
2699-
$this->storeResult($storage, $expr, $result);
2698+
$this->storeBeforeScope($storage, $expr, $result);
27002699

27012700
return $result;
27022701
}
@@ -2707,7 +2706,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
27072706
);
27082707
$scope = $result->getScope();
27092708
if (!$expr instanceof Expr\AssignOp\Coalesce) {
2710-
$this->storeResult($storage, $expr, $result);
2709+
$this->storeBeforeScope($storage, $expr, $result);
27112710
}
27122711
$hasYield = $result->hasYield();
27132712
$throwPoints = $result->getThrowPoints();
@@ -3236,7 +3235,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
32363235
static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
32373236
static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
32383237
);
3239-
$this->storeResult($storage, $expr, $result);
3238+
$this->storeBeforeScope($storage, $expr, $result);
32403239

32413240
return $result;
32423241
} elseif ($expr instanceof StaticCall) {
@@ -3456,7 +3455,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
34563455
static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
34573456
static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
34583457
);
3459-
$this->storeResult($storage, $expr, $result);
3458+
$this->storeBeforeScope($storage, $expr, $result);
34603459

34613460
return $result;
34623461
} elseif ($expr instanceof StaticPropertyFetch) {
@@ -3501,7 +3500,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
35013500
[],
35023501
[],
35033502
);
3504-
$this->storeResult($storage, $expr, $result);
3503+
$this->storeBeforeScope($storage, $expr, $result);
35053504

35063505
return $result;
35073506
} elseif ($expr instanceof Expr\ArrowFunction) {
@@ -3514,7 +3513,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
35143513
[],
35153514
[],
35163515
);
3517-
$this->storeResult($storage, $expr, $exprResult);
3516+
$this->storeBeforeScope($storage, $expr, $exprResult);
35183517
return $exprResult;
35193518
} elseif ($expr instanceof ErrorSuppress) {
35203519
$result = $this->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context);
@@ -3623,7 +3622,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36233622
static fn (): MutatingScope => $rightResult->getScope()->filterByTruthyValue($expr),
36243623
static fn (): MutatingScope => $leftMergedWithRightScope->filterByFalseyValue($expr),
36253624
);
3626-
$this->storeResult($storage, $expr, $result);
3625+
$this->storeBeforeScope($storage, $expr, $result);
36273626
return $result;
36283627
} elseif ($expr instanceof BooleanOr || $expr instanceof BinaryOp\LogicalOr) {
36293628
$leftResult = $this->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep());
@@ -3647,7 +3646,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36473646
static fn (): MutatingScope => $leftMergedWithRightScope->filterByTruthyValue($expr),
36483647
static fn (): MutatingScope => $rightResult->getScope()->filterByFalseyValue($expr),
36493648
);
3650-
$this->storeResult($storage, $expr, $result);
3649+
$this->storeBeforeScope($storage, $expr, $result);
36513650
return $result;
36523651
} elseif ($expr instanceof Coalesce) {
36533652
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left);
@@ -4121,7 +4120,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
41214120
static fn (): MutatingScope => $finalScope->filterByTruthyValue($expr),
41224121
static fn (): MutatingScope => $finalScope->filterByFalseyValue($expr),
41234122
);
4124-
$this->storeResult($storage, $expr, $result);
4123+
$this->storeBeforeScope($storage, $expr, $result);
41254124
return $result;
41264125

41274126
} elseif ($expr instanceof Expr\Yield_) {
@@ -4477,7 +4476,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
44774476
static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
44784477
static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
44794478
);
4480-
$this->storeResult($storage, $expr, $result);
4479+
$this->storeBeforeScope($storage, $expr, $result);
44814480
return $result;
44824481
}
44834482

@@ -5668,7 +5667,7 @@ private function processArgs(
56685667
$isAlwaysTerminating = $isAlwaysTerminating || $closureResult->isAlwaysTerminating();
56695668
}
56705669

5671-
$this->storeResult($storage, $arg->value, new ExpressionResult($closureResult->getScope(), $scopeToPass, false, $isAlwaysTerminating, $throwPoints, $impurePoints));
5670+
$this->storeBeforeScope($storage, $arg->value, new ExpressionResult($closureResult->getScope(), $scopeToPass, false, $isAlwaysTerminating, $throwPoints, $impurePoints));
56725671

56735672
$uses = [];
56745673
foreach ($arg->value->uses as $use) {
@@ -5724,7 +5723,7 @@ private function processArgs(
57245723
$impurePoints = array_merge($impurePoints, $arrowFunctionResult->getImpurePoints());
57255724
$isAlwaysTerminating = $isAlwaysTerminating || $arrowFunctionResult->isAlwaysTerminating();
57265725
}
5727-
$this->storeResult($storage, $arg->value, new ExpressionResult($arrowFunctionResult->getScope(), $scopeToPass, false, $isAlwaysTerminating, $throwPoints, $impurePoints));
5726+
$this->storeBeforeScope($storage, $arg->value, new ExpressionResult($arrowFunctionResult->getScope(), $scopeToPass, false, $isAlwaysTerminating, $throwPoints, $impurePoints));
57285727
} else {
57295728
$exprType = $scope->getType($arg->value);
57305729
$exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context->enterDeep());

0 commit comments

Comments
 (0)