Skip to content

Commit 5d61f3f

Browse files
committed
Do not change what $expr is so that it's added to the storage
1 parent 162f630 commit 5d61f3f

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,25 +2774,26 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
27742774
);
27752775
}
27762776

2777+
$normalizedExpr = $expr;
27772778
if ($parametersAcceptor !== null) {
2778-
$expr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
2779+
$normalizedExpr = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $expr) ?? $expr;
27792780
$returnType = $parametersAcceptor->getReturnType();
27802781
$isAlwaysTerminating = $isAlwaysTerminating || $returnType instanceof NeverType && $returnType->isExplicit();
27812782
}
27822783

27832784
if (
2784-
$expr->name instanceof Name
2785+
$normalizedExpr->name instanceof Name
27852786
&& $functionReflection !== null
27862787
&& $functionReflection->getName() === 'clone'
2787-
&& count($expr->getArgs()) === 2
2788+
&& count($normalizedExpr->getArgs()) === 2
27882789
) {
2789-
$clonePropertiesArgType = $scope->getType($expr->getArgs()[1]->value);
2790-
$cloneExpr = new TypeExpr($scope->getType(new Expr\Clone_($expr->getArgs()[0]->value)));
2790+
$clonePropertiesArgType = $scope->getType($normalizedExpr->getArgs()[1]->value);
2791+
$cloneExpr = new TypeExpr($scope->getType(new Expr\Clone_($normalizedExpr->getArgs()[0]->value)));
27912792
$clonePropertiesArgTypeConstantArrays = $clonePropertiesArgType->getConstantArrays();
27922793
foreach ($clonePropertiesArgTypeConstantArrays as $clonePropertiesArgTypeConstantArray) {
27932794
foreach ($clonePropertiesArgTypeConstantArray->getKeyTypes() as $i => $clonePropertyKeyType) {
27942795
$clonePropertyKeyTypeScalars = $clonePropertyKeyType->getConstantScalarValues();
2795-
$propertyAttributes = $expr->getAttributes();
2796+
$propertyAttributes = $normalizedExpr->getAttributes();
27962797
$propertyAttributes['inCloneWith'] = true;
27972798
if (count($clonePropertyKeyTypeScalars) === 1) {
27982799
$this->processVirtualAssign(
@@ -2818,23 +2819,23 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
28182819
}
28192820
}
28202821

2821-
$result = $this->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $expr, $scope, $storage, $nodeCallback, $context);
2822+
$result = $this->processArgs($stmt, $functionReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context);
28222823
$scope = $result->getScope();
28232824
$hasYield = $result->hasYield();
28242825
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());
28252826
$impurePoints = array_merge($impurePoints, $result->getImpurePoints());
28262827
$isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating();
28272828

2828-
if ($expr->name instanceof Expr) {
2829-
$nameType = $scope->getType($expr->name);
2829+
if ($normalizedExpr->name instanceof Expr) {
2830+
$nameType = $scope->getType($normalizedExpr->name);
28302831
if (
28312832
$nameType->isObject()->yes()
28322833
&& $nameType->isCallable()->yes()
28332834
&& (new ObjectType(Closure::class))->isSuperTypeOf($nameType)->no()
28342835
) {
28352836
$invokeResult = $this->processExprNode(
28362837
$stmt,
2837-
new MethodCall($expr->name, '__invoke', $expr->getArgs(), $expr->getAttributes()),
2838+
new MethodCall($normalizedExpr->name, '__invoke', $normalizedExpr->getArgs(), $normalizedExpr->getAttributes()),
28382839
$scope,
28392840
$storage,
28402841
new NoopNodeCallback(),
@@ -2847,7 +2848,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
28472848
}
28482849

28492850
if ($functionReflection !== null) {
2850-
$functionThrowPoint = $this->getFunctionThrowPoint($functionReflection, $parametersAcceptor, $expr, $scope);
2851+
$functionThrowPoint = $this->getFunctionThrowPoint($functionReflection, $parametersAcceptor, $normalizedExpr, $scope);
28512852
if ($functionThrowPoint !== null) {
28522853
$throwPoints[] = $functionThrowPoint;
28532854
}
@@ -2875,18 +2876,18 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
28752876
if (
28762877
$functionReflection !== null
28772878
&& $functionReflection->getName() === 'file_put_contents'
2878-
&& count($expr->getArgs()) > 0
2879+
&& count($normalizedExpr->getArgs()) > 0
28792880
) {
2880-
$scope = $scope->invalidateExpression(new FuncCall(new Name('file_get_contents'), [$expr->getArgs()[0]]))
2881-
->invalidateExpression(new FuncCall(new Name\FullyQualified('file_get_contents'), [$expr->getArgs()[0]]));
2881+
$scope = $scope->invalidateExpression(new FuncCall(new Name('file_get_contents'), [$normalizedExpr->getArgs()[0]]))
2882+
->invalidateExpression(new FuncCall(new Name\FullyQualified('file_get_contents'), [$normalizedExpr->getArgs()[0]]));
28822883
}
28832884

28842885
if (
28852886
$functionReflection !== null
28862887
&& in_array($functionReflection->getName(), ['array_pop', 'array_shift'], true)
2887-
&& count($expr->getArgs()) >= 1
2888+
&& count($normalizedExpr->getArgs()) >= 1
28882889
) {
2889-
$arrayArg = $expr->getArgs()[0]->value;
2890+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
28902891

28912892
$arrayArgType = $scope->getType($arrayArg);
28922893
$arrayArgNativeType = $scope->getNativeType($arrayArg);
@@ -2908,18 +2909,18 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29082909
if (
29092910
$functionReflection !== null
29102911
&& in_array($functionReflection->getName(), ['array_push', 'array_unshift'], true)
2911-
&& count($expr->getArgs()) >= 2
2912+
&& count($normalizedExpr->getArgs()) >= 2
29122913
) {
2913-
$arrayArg = $expr->getArgs()[0]->value;
2914+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
29142915

29152916
$scope = $this->processVirtualAssign(
29162917
$scope,
29172918
$storage,
29182919
$stmt,
29192920
$arrayArg,
29202921
new NativeTypeExpr(
2921-
$this->getArrayFunctionAppendingType($functionReflection, $scope, $expr),
2922-
$this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $expr),
2922+
$this->getArrayFunctionAppendingType($functionReflection, $scope, $normalizedExpr),
2923+
$this->getArrayFunctionAppendingType($functionReflection, $scope->doNotTreatPhpDocTypesAsCertain(), $normalizedExpr),
29232924
),
29242925
$nodeCallback,
29252926
)->getScope();
@@ -2936,7 +2937,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29362937
$functionReflection !== null
29372938
&& $functionReflection->getName() === 'shuffle'
29382939
) {
2939-
$arrayArg = $expr->getArgs()[0]->value;
2940+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
29402941

29412942
$scope = $this->processVirtualAssign(
29422943
$scope,
@@ -2951,15 +2952,15 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29512952
if (
29522953
$functionReflection !== null
29532954
&& $functionReflection->getName() === 'array_splice'
2954-
&& count($expr->getArgs()) >= 2
2955+
&& count($normalizedExpr->getArgs()) >= 2
29552956
) {
2956-
$arrayArg = $expr->getArgs()[0]->value;
2957+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
29572958
$arrayArgType = $scope->getType($arrayArg);
29582959
$arrayArgNativeType = $scope->getNativeType($arrayArg);
29592960

2960-
$offsetType = $scope->getType($expr->getArgs()[1]->value);
2961-
$lengthType = isset($expr->getArgs()[2]) ? $scope->getType($expr->getArgs()[2]->value) : new NullType();
2962-
$replacementType = isset($expr->getArgs()[3]) ? $scope->getType($expr->getArgs()[3]->value) : new ConstantArrayType([], []);
2961+
$offsetType = $scope->getType($normalizedExpr->getArgs()[1]->value);
2962+
$lengthType = isset($normalizedExpr->getArgs()[2]) ? $scope->getType($normalizedExpr->getArgs()[2]->value) : new NullType();
2963+
$replacementType = isset($normalizedExpr->getArgs()[3]) ? $scope->getType($normalizedExpr->getArgs()[3]->value) : new ConstantArrayType([], []);
29632964

29642965
$scope = $this->processVirtualAssign(
29652966
$scope,
@@ -2977,9 +2978,9 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29772978
if (
29782979
$functionReflection !== null
29792980
&& in_array($functionReflection->getName(), ['sort', 'rsort', 'usort'], true)
2980-
&& count($expr->getArgs()) >= 1
2981+
&& count($normalizedExpr->getArgs()) >= 1
29812982
) {
2982-
$arrayArg = $expr->getArgs()[0]->value;
2983+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
29832984

29842985
$scope = $this->processVirtualAssign(
29852986
$scope,
@@ -2994,9 +2995,9 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
29942995
if (
29952996
$functionReflection !== null
29962997
&& in_array($functionReflection->getName(), ['natcasesort', 'natsort', 'arsort', 'asort', 'ksort', 'krsort', 'uasort', 'uksort'], true)
2997-
&& count($expr->getArgs()) >= 1
2998+
&& count($normalizedExpr->getArgs()) >= 1
29982999
) {
2999-
$arrayArg = $expr->getArgs()[0]->value;
3000+
$arrayArg = $normalizedExpr->getArgs()[0]->value;
30003001

30013002
$scope = $this->processVirtualAssign(
30023003
$scope,
@@ -3012,7 +3013,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
30123013
$functionReflection !== null
30133014
&& $functionReflection->getName() === 'extract'
30143015
) {
3015-
$extractedArg = $expr->getArgs()[0]->value;
3016+
$extractedArg = $normalizedExpr->getArgs()[0]->value;
30163017
$extractedType = $scope->getType($extractedArg);
30173018
$constantArrays = $extractedType->getConstantArrays();
30183019
if (count($constantArrays) > 0) {
@@ -3126,8 +3127,9 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
31263127
);
31273128
}
31283129

3130+
$normalizedExpr = $expr;
31293131
if ($parametersAcceptor !== null) {
3130-
$expr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
3132+
$normalizedExpr = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $expr) ?? $expr;
31313133
$returnType = $parametersAcceptor->getReturnType();
31323134
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
31333135
}
@@ -3137,7 +3139,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
31373139
$methodReflection,
31383140
$methodReflection !== null ? $scope->getNakedMethod($calledOnType, $methodReflection->getName()) : null,
31393141
$parametersAcceptor,
3140-
$expr,
3142+
$normalizedExpr,
31413143
$scope,
31423144
$storage,
31433145
$nodeCallback,
@@ -3148,21 +3150,21 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
31483150
if ($methodReflection !== null) {
31493151
$hasSideEffects = $methodReflection->hasSideEffects();
31503152
if ($hasSideEffects->yes() || $methodReflection->getName() === '__construct') {
3151-
$this->callNodeCallback($nodeCallback, new InvalidateExprNode($expr->var), $scope, $storage);
3152-
$scope = $scope->invalidateExpression($expr->var, true);
3153+
$this->callNodeCallback($nodeCallback, new InvalidateExprNode($normalizedExpr->var), $scope, $storage);
3154+
$scope = $scope->invalidateExpression($normalizedExpr->var, true);
31533155
}
31543156
if ($parametersAcceptor !== null && !$methodReflection->isStatic()) {
31553157
$selfOutType = $methodReflection->getSelfOutType();
31563158
if ($selfOutType !== null) {
31573159
$scope = $scope->assignExpression(
3158-
$expr->var,
3160+
$normalizedExpr->var,
31593161
TemplateTypeHelper::resolveTemplateTypes(
31603162
$selfOutType,
31613163
$parametersAcceptor->getResolvedTemplateTypeMap(),
31623164
$parametersAcceptor instanceof ExtendedParametersAcceptor ? $parametersAcceptor->getCallSiteVarianceMap() : TemplateTypeVarianceMap::createEmpty(),
31633165
TemplateTypeVariance::createCovariant(),
31643166
),
3165-
$scope->getNativeType($expr->var),
3167+
$scope->getNativeType($normalizedExpr->var),
31663168
);
31673169
}
31683170
}
@@ -3327,12 +3329,13 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
33273329
);
33283330
}
33293331

3332+
$normalizedExpr = $expr;
33303333
if ($parametersAcceptor !== null) {
3331-
$expr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
3334+
$normalizedExpr = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $expr) ?? $expr;
33323335
$returnType = $parametersAcceptor->getReturnType();
33333336
$isAlwaysTerminating = $returnType instanceof NeverType && $returnType->isExplicit();
33343337
}
3335-
$result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $expr, $scope, $storage, $nodeCallback, $context, $closureBindScope ?? null);
3338+
$result = $this->processArgs($stmt, $methodReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context, $closureBindScope ?? null);
33363339
$scope = $result->getScope();
33373340
$scopeFunction = $scope->getFunction();
33383341

@@ -3871,6 +3874,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
38713874
$impurePoints = [];
38723875
$isAlwaysTerminating = false;
38733876
$className = null;
3877+
$normalizedExpr = $expr;
38743878
if ($expr->class instanceof Expr || $expr->class instanceof Name) {
38753879
if ($expr->class instanceof Expr) {
38763880
$objectClasses = $scope->getType($expr)->getObjectClassNames();
@@ -3937,7 +3941,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
39373941
}
39383942

39393943
if ($parametersAcceptor !== null) {
3940-
$expr = ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $expr) ?? $expr;
3944+
$normalizedExpr = ArgumentsNormalizer::reorderNewArguments($parametersAcceptor, $expr) ?? $expr;
39413945
}
39423946

39433947
} else {
@@ -4001,7 +4005,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
40014005
}
40024006
}
40034007

4004-
$result = $this->processArgs($stmt, $constructorReflection, null, $parametersAcceptor, $expr, $scope, $storage, $nodeCallback, $context);
4008+
$result = $this->processArgs($stmt, $constructorReflection, null, $parametersAcceptor, $normalizedExpr, $scope, $storage, $nodeCallback, $context);
40054009
$scope = $result->getScope();
40064010
$hasYield = $hasYield || $result->hasYield();
40074011
$throwPoints = array_merge($throwPoints, $result->getThrowPoints());

0 commit comments

Comments
 (0)