Skip to content

Commit 4d119ba

Browse files
committed
Simplify many processAssignVar with new processVirtualAssign method
1 parent ab76a87 commit 4d119ba

File tree

1 file changed

+41
-106
lines changed

1 file changed

+41
-106
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 41 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,22 +1913,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch
19131913

19141914
/** @var Expr $clonedVar */
19151915
[$clonedVar] = $traverser->traverse([$clonedVar]);
1916-
$scope = $this->processAssignVar(
1917-
$scope,
1918-
$stmt,
1919-
$clonedVar,
1920-
new UnsetOffsetExpr($var->var, $var->dim),
1921-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
1922-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
1923-
return;
1924-
}
1925-
1926-
$nodeCallback($node, $scope);
1927-
},
1928-
ExpressionContext::createDeep(),
1929-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
1930-
false,
1931-
)->getScope();
1916+
$scope = $this->processVirtualAssign($scope, $stmt, $clonedVar, new UnsetOffsetExpr($var->var, $var->dim), $nodeCallback)->getScope();
19321917
} elseif ($var instanceof PropertyFetch) {
19331918
$scope = $scope->invalidateExpression($var);
19341919
$impurePoints[] = new ImpurePoint(
@@ -2674,24 +2659,15 @@ static function (): void {
26742659
$arrayArgNativeType = $scope->getNativeType($arrayArg);
26752660
$isArrayPop = $functionReflection->getName() === 'array_pop';
26762661

2677-
$scope = $this->processAssignVar(
2662+
$scope = $this->processVirtualAssign(
26782663
$scope,
26792664
$stmt,
26802665
$arrayArg,
26812666
new NativeTypeExpr(
26822667
$isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray(),
26832668
$isArrayPop ? $arrayArgNativeType->popArray() : $arrayArgNativeType->shiftArray(),
26842669
),
2685-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2686-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2687-
return;
2688-
}
2689-
2690-
$nodeCallback($node, $scope);
2691-
},
2692-
$context,
2693-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2694-
true,
2670+
$nodeCallback,
26952671
)->getScope();
26962672
}
26972673

@@ -2702,24 +2678,15 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27022678
) {
27032679
$arrayArg = $expr->getArgs()[0]->value;
27042680

2705-
$scope = $this->processAssignVar(
2681+
$scope = $this->processVirtualAssign(
27062682
$scope,
27072683
$stmt,
27082684
$arrayArg,
27092685
new NativeTypeExpr(
27102686
$this->getArrayFunctionAppendingType($functionReflection, $scope, $expr),
27112687
$this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $expr),
27122688
),
2713-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2714-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2715-
return;
2716-
}
2717-
2718-
$nodeCallback($node, $scope);
2719-
},
2720-
$context,
2721-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2722-
true,
2689+
$nodeCallback,
27232690
)->getScope();
27242691
}
27252692

@@ -2736,21 +2703,12 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27362703
) {
27372704
$arrayArg = $expr->getArgs()[0]->value;
27382705

2739-
$scope = $this->processAssignVar(
2706+
$scope = $this->processVirtualAssign(
27402707
$scope,
27412708
$stmt,
27422709
$arrayArg,
27432710
new NativeTypeExpr($scope->getType($arrayArg)->shuffleArray(), $scope->getNativeType($arrayArg)->shuffleArray()),
2744-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2745-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2746-
return;
2747-
}
2748-
2749-
$nodeCallback($node, $scope);
2750-
},
2751-
$context,
2752-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2753-
true,
2711+
$nodeCallback,
27542712
)->getScope();
27552713
}
27562714

@@ -2767,24 +2725,15 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27672725
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
27682726
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
27692727

2770-
$scope = $this->processAssignVar(
2728+
$scope = $this->processVirtualAssign(
27712729
$scope,
27722730
$stmt,
27732731
$arrayArg,
27742732
new NativeTypeExpr(
27752733
$arrayArgType->spliceArray($offsetType, $lengthType, $replacementType),
27762734
$arrayArgNativeType->spliceArray($offsetType, $lengthType, $replacementType),
27772735
),
2778-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2779-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2780-
return;
2781-
}
2782-
2783-
$nodeCallback($node, $scope);
2784-
},
2785-
$context,
2786-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2787-
true,
2736+
$nodeCallback,
27882737
)->getScope();
27892738
}
27902739

@@ -2795,21 +2744,12 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
27952744
) {
27962745
$arrayArg = $expr->getArgs()[0]->value;
27972746

2798-
$scope = $this->processAssignVar(
2747+
$scope = $this->processVirtualAssign(
27992748
$scope,
28002749
$stmt,
28012750
$arrayArg,
28022751
new NativeTypeExpr($this->getArraySortPreserveListFunctionType($scope->getType($arrayArg)), $this->getArraySortPreserveListFunctionType($scope->getNativeType($arrayArg))),
2803-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2804-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2805-
return;
2806-
}
2807-
2808-
$nodeCallback($node, $scope);
2809-
},
2810-
$context,
2811-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2812-
true,
2752+
$nodeCallback,
28132753
)->getScope();
28142754
}
28152755

@@ -2820,21 +2760,12 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
28202760
) {
28212761
$arrayArg = $expr->getArgs()[0]->value;
28222762

2823-
$scope = $this->processAssignVar(
2763+
$scope = $this->processVirtualAssign(
28242764
$scope,
28252765
$stmt,
28262766
$arrayArg,
28272767
new NativeTypeExpr($this->getArraySortDoNotPreserveListFunctionType($scope->getType($arrayArg)), $this->getArraySortDoNotPreserveListFunctionType($scope->getNativeType($arrayArg))),
2828-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
2829-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
2830-
return;
2831-
}
2832-
2833-
$nodeCallback($node, $scope);
2834-
},
2835-
$context,
2836-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
2837-
true,
2768+
$nodeCallback,
28382769
)->getScope();
28392770
}
28402771

@@ -3768,21 +3699,12 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
37683699
$newExpr = new Expr\PreDec($expr->var);
37693700
}
37703701

3771-
$scope = $this->processAssignVar(
3702+
$scope = $this->processVirtualAssign(
37723703
$scope,
37733704
$stmt,
37743705
$expr->var,
37753706
$newExpr,
3776-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
3777-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
3778-
return;
3779-
}
3780-
3781-
$nodeCallback($node, $scope);
3782-
},
3783-
$context,
3784-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
3785-
false,
3707+
$nodeCallback,
37863708
)->getScope();
37873709
} elseif ($expr instanceof Ternary) {
37883710
$ternaryCondResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $context->enterDeep());
@@ -5469,23 +5391,13 @@ private function processArgs(
54695391
$byRefType = $paramOutType;
54705392
}
54715393

5472-
$result = $this->processAssignVar(
5394+
$scope = $this->processVirtualAssign(
54735395
$scope,
54745396
$stmt,
54755397
$argValue,
54765398
new TypeExpr($byRefType),
5477-
static function (Node $node, Scope $scope) use ($nodeCallback): void {
5478-
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
5479-
return;
5480-
}
5481-
5482-
$nodeCallback($node, $scope);
5483-
},
5484-
$context,
5485-
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
5486-
true,
5487-
);
5488-
$scope = $result->getScope();
5399+
$nodeCallback,
5400+
)->getScope();
54895401
}
54905402
} elseif ($calleeReflection !== null && $calleeReflection->hasSideEffects()->yes()) {
54915403
$argType = $scope->getType($arg->value);
@@ -6162,6 +6074,29 @@ static function (): void {
61626074
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
61636075
}
61646076

6077+
/**
6078+
* @param callable(Node $node, Scope $scope): void $nodeCallback
6079+
*/
6080+
private function processVirtualAssign(MutatingScope $scope, Node\Stmt $stmt, Expr $var, Expr $assignedExpr, callable $nodeCallback): ExpressionResult
6081+
{
6082+
return $this->processAssignVar(
6083+
$scope,
6084+
$stmt,
6085+
$var,
6086+
$assignedExpr,
6087+
static function (Node $node, Scope $scope) use ($nodeCallback): void {
6088+
if (!$node instanceof PropertyAssignNode && !$node instanceof VariableAssignNode) {
6089+
return;
6090+
}
6091+
6092+
$nodeCallback($node, $scope);
6093+
},
6094+
ExpressionContext::createDeep(),
6095+
static fn (MutatingScope $scope): ExpressionResult => new ExpressionResult($scope, false, false, [], []),
6096+
false,
6097+
);
6098+
}
6099+
61656100
/**
61666101
* @param list<ArrayDimFetch> $dimFetchStack
61676102
*/

0 commit comments

Comments
 (0)