Skip to content

Commit c0b8d16

Browse files
committed
Optional types in ExpressionResult
1 parent 36ca42d commit c0b8d16

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Analyser/ExpressionResult.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPStan\Analyser;
44

5+
use PHPStan\Type\Type;
6+
57
final class ExpressionResult
68
{
79

@@ -29,12 +31,24 @@ public function __construct(
2931
private array $impurePoints,
3032
?callable $truthyScopeCallback = null,
3133
?callable $falseyScopeCallback = null,
34+
private ?Type $type = null,
35+
private ?Type $nativeType = null,
3236
)
3337
{
3438
$this->truthyScopeCallback = $truthyScopeCallback;
3539
$this->falseyScopeCallback = $falseyScopeCallback;
3640
}
3741

42+
public function getType(): ?Type
43+
{
44+
return $this->type;
45+
}
46+
47+
public function getNativeType(): ?Type
48+
{
49+
return $this->nativeType;
50+
}
51+
3852
public function getScope(): MutatingScope
3953
{
4054
return $this->scope;

src/Analyser/NodeScopeResolver.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,9 @@ public function processExprNode(
25612561

25622562
$this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $context);
25632563

2564+
$expressionType = null;
2565+
$expressionNativeType = null;
2566+
25642567
if ($expr instanceof Variable) {
25652568
$hasYield = false;
25662569
$throwPoints = [];
@@ -2619,7 +2622,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
26192622
$scope = $scope->exitExpressionAssign($expr->expr);
26202623
}
26212624

2622-
$result = new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
2625+
$result = new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints, type: $result->getType(), nativeType: $result->getNativeType());
26232626
$storage->storeResult($expr, $result);
26242627

26252628
return $result;
@@ -4381,6 +4384,8 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
43814384
$throwPoints = [];
43824385
$impurePoints = [];
43834386
$isAlwaysTerminating = false;
4387+
$expressionType = $this->initializerExprTypeResolver->getType($expr, InitializerExprContext::fromScope($scope));
4388+
$expressionNativeType = $expressionType;
43844389
} elseif ($expr instanceof ConstFetch) {
43854390
$hasYield = false;
43864391
$throwPoints = [];
@@ -4402,6 +4407,8 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
44024407
$impurePoints,
44034408
static fn (): MutatingScope => $scope->filterByTruthyValue($expr),
44044409
static fn (): MutatingScope => $scope->filterByFalseyValue($expr),
4410+
$expressionType,
4411+
$expressionNativeType,
44054412
);
44064413
$storage->storeResult($expr, $result);
44074414
return $result;
@@ -5872,12 +5879,16 @@ private function processAssignVar(
58725879
$impurePoints = [];
58735880
$isAlwaysTerminating = false;
58745881
$isAssignOp = $assignedExpr instanceof Expr\AssignOp && !$enterExpressionAssign;
5882+
$expressionType = null;
5883+
$expressionNativeType = null;
58755884
if ($var instanceof Variable && is_string($var->name)) {
58765885
$result = $processExprCallback($scope);
58775886
$hasYield = $result->hasYield();
58785887
$throwPoints = $result->getThrowPoints();
58795888
$impurePoints = $result->getImpurePoints();
58805889
$isAlwaysTerminating = $result->isAlwaysTerminating();
5890+
$expressionType = $result->getType();
5891+
$expressionNativeType = $result->getNativeType();
58815892
if (in_array($var->name, Scope::SUPERGLOBAL_VARIABLES, true)) {
58825893
$impurePoints[] = new ImpurePoint($scope, $var, 'superglobal', 'assign to superglobal variable', true);
58835894
}
@@ -6399,7 +6410,7 @@ private function processAssignVar(
63996410
}
64006411

64016412
// stored where processAssignVar is called
6402-
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
6413+
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints, type: $expressionType, nativeType: $expressionNativeType);
64036414
}
64046415

64056416
/**

0 commit comments

Comments
 (0)