Skip to content

Commit a512714

Browse files
committed
ExpressionResultStorage
1 parent d08d0c2 commit a512714

File tree

5 files changed

+389
-234
lines changed

5 files changed

+389
-234
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PhpParser\Node\Expr;
6+
use PHPStan\ShouldNotHappenException;
7+
use SplObjectStorage;
8+
use function get_class;
9+
use function sprintf;
10+
11+
final class ExpressionResultStorage
12+
{
13+
14+
/** @var SplObjectStorage<Expr, ExpressionResult> */
15+
private SplObjectStorage $results;
16+
17+
public function __construct()
18+
{
19+
$this->results = new SplObjectStorage();
20+
}
21+
22+
public function duplicate(): self
23+
{
24+
$new = new self();
25+
$new->results->addAll($this->results);
26+
return $new;
27+
}
28+
29+
public function storeResult(Expr $expr, ExpressionResult $result): void
30+
{
31+
$this->results[$expr] = $result;
32+
}
33+
34+
public function findResult(Expr $expr): ?ExpressionResult
35+
{
36+
return $this->results[$expr] ?? null;
37+
}
38+
39+
public function getResult(Expr $expr): ExpressionResult
40+
{
41+
if (!isset($this->results[$expr])) {
42+
throw new ShouldNotHappenException(sprintf('Result for expr %s not found', get_class($expr)));
43+
}
44+
45+
return $this->results[$expr];
46+
}
47+
48+
}

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)