Skip to content

Commit b931d47

Browse files
committed
ExpressionResultStorage
1 parent d08d0c2 commit b931d47

File tree

5 files changed

+377
-234
lines changed

5 files changed

+377
-234
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node\Expr;
6+
use SplObjectStorage;
7+
8+
final class ExpressionResultStorage
9+
{
10+
11+
/** @var SplObjectStorage<Expr, ExpressionResult> */
12+
private SplObjectStorage $results;
13+
14+
public function __construct()
15+
{
16+
$this->results = new SplObjectStorage();
17+
}
18+
19+
public function duplicate(): self
20+
{
21+
$new = new self();
22+
$new->results->addAll($this->results);
23+
return $new;
24+
}
25+
26+
public function storeResult(Expr $expr, ExpressionResult $result): void
27+
{
28+
$this->results[$expr] = $result;
29+
}
30+
31+
public function findResult(Expr $expr): ?ExpressionResult
32+
{
33+
return $this->results[$expr] ?? null;
34+
}
35+
36+
}

src/Analyser/MutatingScope.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,7 @@ private function resolveType(string $exprString, Expr $node): Type
982982
}
983983

984984
if ($this->getBooleanExpressionDepth($node->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) {
985-
$noopCallback = static function (): void {
986-
};
987-
$leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep());
985+
$leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep());
988986
$rightBooleanType = $leftResult->getTruthyScope()->getType($node->right)->toBoolean();
989987
} else {
990988
$rightBooleanType = $this->filterByTruthyValue($node->left)->getType($node->right)->toBoolean();
@@ -1014,9 +1012,7 @@ private function resolveType(string $exprString, Expr $node): Type
10141012
}
10151013

10161014
if ($this->getBooleanExpressionDepth($node->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) {
1017-
$noopCallback = static function (): void {
1018-
};
1019-
$leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep());
1015+
$leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep());
10201016
$rightBooleanType = $leftResult->getFalseyScope()->getType($node->right)->toBoolean();
10211017
} else {
10221018
$rightBooleanType = $this->filterByFalseyValue($node->left)->getType($node->right)->toBoolean();
@@ -1406,6 +1402,7 @@ private function resolveType(string $exprString, Expr $node): Type
14061402
new Node\Stmt\Expression($node->expr),
14071403
$node->expr,
14081404
$arrowScope,
1405+
new ExpressionResultStorage(),
14091406
static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpurePoints, &$invalidateExpressions): void {
14101407
if ($scope->getAnonymousFunctionReflection() !== $arrowScope->getAnonymousFunctionReflection()) {
14111408
return;
@@ -2042,9 +2039,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
20422039
}
20432040

20442041
if ($node instanceof Expr\Ternary) {
2045-
$noopCallback = static function (): void {
2046-
};
2047-
$condResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->cond), $node->cond, $this, $noopCallback, ExpressionContext::createDeep());
2042+
$condResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->cond), $node->cond, $this, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep());
20482043
if ($node->if === null) {
20492044
$conditionType = $this->getType($node->cond);
20502045
$booleanConditionType = $conditionType->toBoolean();

0 commit comments

Comments
 (0)