Skip to content

Commit fc0361a

Browse files
ondrejmirtesclaude
andcommitted
Move processVirtualAssign() from NodeScopeResolver to AssignHandler
The virtual assign is a thin wrapper over AssignHandler::prepareTarget() and applyWrite(), so it now lives next to them as a public method and uses the handler's own VirtualExprResultHelper and ExpressionResultFactory instead of container lookups. The inc/dec handlers, FuncCallHandler, FuncCallScopeEffectsHelper, ForeachHandler, UnsetHandler and ArgumentsHandler inject AssignHandler. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014hgcbwfKQnye2coQXtX5XU
1 parent 4d89547 commit fc0361a

11 files changed

Lines changed: 105 additions & 76 deletions

‎src/Analyser/ArgumentsHandler.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpParser\Node\Identifier;
1515
use PhpParser\Node\Name;
1616
use PhpParser\NodeFinder;
17+
use PHPStan\Analyser\ExprHandler\AssignHandler;
1718
use PHPStan\Analyser\ExprHandler\ClosureHandler;
1819
use PHPStan\Analyser\ExprHandler\Helper\ClosureParameterResolver;
1920
use PHPStan\Analyser\ExprHandler\Helper\ClosureTypeResolver;
@@ -117,6 +118,7 @@ public function __construct(
117118
private ExtensionsCollection $staticMethodParameterClosureTypeExtensions,
118119
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
119120
private bool $implicitThrows,
121+
private AssignHandler $assignHandler,
120122
)
121123
{
122124
}
@@ -769,7 +771,8 @@ public function processArgs(
769771
$byRefType = new MixedType();
770772
}
771773

772-
$scope = $nodeScopeResolver->processVirtualAssign(
774+
$scope = $this->assignHandler->processVirtualAssign(
775+
$nodeScopeResolver,
773776
$scope,
774777
$storage,
775778
$stmt,

‎src/Analyser/ExprHandler/AssignHandler.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@
5151
use PHPStan\Analyser\VarAnnotationProcessor;
5252
use PHPStan\Analyser\VariableFlow;
5353
use PHPStan\Analyser\VariableFlowBuilder;
54+
use PHPStan\Analyser\VirtualAssignNodeCallback;
5455
use PHPStan\DependencyInjection\AutowiredService;
5556
use PHPStan\Node\Expr\ExistingArrayDimFetch;
5657
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
58+
use PHPStan\Node\Expr\NativeTypeExpr;
5759
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
5860
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
5961
use PHPStan\Node\Expr\TypeExpr;
@@ -547,6 +549,59 @@ private function createSpecifyTypesCallback(Assign $expr, ExpressionResult $assi
547549
};
548550
}
549551

552+
/**
553+
* @param callable(Node $node, Scope $scope): void $nodeCallback
554+
*/
555+
public function processVirtualAssign(NodeScopeResolver $nodeScopeResolver, MutatingScope $scope, ExpressionResultStorage $storage, Node\Stmt $stmt, Expr $var, Expr $assignedExpr, callable $nodeCallback, ?ExpressionResult $assignedExprResult = null): ExpressionResult
556+
{
557+
// work off an available result for the assigned expr: passed by the
558+
// caller, or fabricated from a type-carrying virtual node - threaded
559+
// straight into applyWrite() so its reads compose instead of falling
560+
// back to on-demand pricing of the type, the truthy/falsey narrowing,
561+
// and the synthetic sentinel comparisons
562+
if (
563+
$assignedExprResult === null
564+
&& ($assignedExpr instanceof TypeExpr || $assignedExpr instanceof NativeTypeExpr)
565+
&& $storage->findExpressionResult($assignedExpr) === null
566+
) {
567+
$assignedExprResult = $this->virtualExprResultHelper->createTypeExprResult($scope, $assignedExpr);
568+
}
569+
570+
$virtualAssignNodeCallback = VirtualAssignNodeCallback::create($nodeCallback);
571+
$target = $this->prepareTarget(
572+
$nodeScopeResolver,
573+
$scope,
574+
$storage,
575+
$stmt,
576+
$var,
577+
$assignedExpr,
578+
$virtualAssignNodeCallback,
579+
ExpressionContext::createDeep(),
580+
AssignTargetWalkMode::virtualAssign(),
581+
);
582+
583+
return $this->applyWrite(
584+
$nodeScopeResolver,
585+
$target,
586+
$this->expressionResultFactory->create(
587+
$target->getScope(),
588+
beforeScope: $target->getScope(),
589+
expr: $assignedExpr,
590+
hasYield: false,
591+
isAlwaysTerminating: false,
592+
throwPoints: [],
593+
impurePoints: [],
594+
typeCallback: static fn () => new MixedType(),
595+
specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(),
596+
),
597+
$assignedExprResult,
598+
$stmt,
599+
$storage,
600+
$virtualAssignNodeCallback,
601+
ExpressionContext::createDeep(),
602+
);
603+
}
604+
550605
/**
551606
* The pre-value half of an assignment: walks the target's sub-expressions
552607
* (root, dimensions, receiver, dynamic name) in PHP's evaluation order and

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function __construct(
107107
private ClosureTypeResolver $closureTypeResolver,
108108
private ArgumentsHandler $argumentsHandler,
109109
private ClosureProcessor $closureProcessor,
110+
private AssignHandler $assignHandler,
110111
)
111112
{
112113
}
@@ -228,7 +229,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
228229
$propertyAttributes = $normalizedExpr->getAttributes();
229230
$propertyAttributes['inCloneWith'] = true;
230231
if (count($clonePropertyKeyTypeScalars) === 1) {
231-
$nodeScopeResolver->processVirtualAssign(
232+
$this->assignHandler->processVirtualAssign(
233+
$nodeScopeResolver,
232234
$scope,
233235
$storage,
234236
$stmt,
@@ -239,7 +241,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
239241
continue;
240242
}
241243

242-
$nodeScopeResolver->processVirtualAssign(
244+
$this->assignHandler->processVirtualAssign(
245+
$nodeScopeResolver,
243246
$scope,
244247
$storage,
245248
$stmt,

‎src/Analyser/ExprHandler/Helper/FuncCallScopeEffectsHelper.php‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt;
1212
use PHPStan\Analyser\ArgsResult;
1313
use PHPStan\Analyser\ExpressionResultStorage;
14+
use PHPStan\Analyser\ExprHandler\AssignHandler;
1415
use PHPStan\Analyser\MutatingScope;
1516
use PHPStan\Analyser\NodeScopeResolver;
1617
use PHPStan\Analyser\Scope;
@@ -64,6 +65,7 @@ public function __construct(
6465
private OutputBufferHelper $outputBufferHelper,
6566
#[AutowiredParameter]
6667
private bool $rememberPossiblyImpureFunctionValues,
68+
private AssignHandler $assignHandler,
6769
)
6870
{
6971
}
@@ -82,7 +84,8 @@ public function applyArrayWalkResult(NodeScopeResolver $nodeScopeResolver, Stmt
8284
$newArrayType = $arrayWalkOriginalArrayType->mapValueType(static fn (Type $type): Type => $arrayWalkValueType);
8385
$newArrayNativeType = $arrayWalkOriginalArrayNativeType->mapValueType(static fn (Type $type): Type => $arrayWalkValueNativeType);
8486

85-
$scope = $nodeScopeResolver->processVirtualAssign(
87+
$scope = $this->assignHandler->processVirtualAssign(
88+
$nodeScopeResolver,
8689
$scope,
8790
$storage,
8891
$stmt,
@@ -169,7 +172,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
169172
$arrayArgNativeType = $arrayArgResult->getTypeOnScope($scope, true);
170173
$isArrayPop = $functionReflection->getName() === 'array_pop';
171174

172-
$scope = $nodeScopeResolver->processVirtualAssign(
175+
$scope = $this->assignHandler->processVirtualAssign(
176+
$nodeScopeResolver,
173177
$scope,
174178
$storage,
175179
$stmt,
@@ -189,7 +193,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
189193
) {
190194
$arrayArg = $normalizedExpr->getArgs()[0]->value;
191195

192-
$scope = $nodeScopeResolver->processVirtualAssign(
196+
$scope = $this->assignHandler->processVirtualAssign(
197+
$nodeScopeResolver,
193198
$scope,
194199
$storage,
195200
$stmt,
@@ -215,7 +220,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
215220
) {
216221
$arrayArg = $normalizedExpr->getArgs()[0]->value;
217222

218-
$scope = $nodeScopeResolver->processVirtualAssign(
223+
$scope = $this->assignHandler->processVirtualAssign(
224+
$nodeScopeResolver,
219225
$scope,
220226
$storage,
221227
$stmt,
@@ -255,7 +261,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
255261
$replacementNativeType = new ConstantArrayType([], []);
256262
}
257263

258-
$scope = $nodeScopeResolver->processVirtualAssign(
264+
$scope = $this->assignHandler->processVirtualAssign(
265+
$nodeScopeResolver,
259266
$scope,
260267
$storage,
261268
$stmt,
@@ -275,7 +282,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
275282
) {
276283
$arrayArg = $normalizedExpr->getArgs()[0]->value;
277284

278-
$scope = $nodeScopeResolver->processVirtualAssign(
285+
$scope = $this->assignHandler->processVirtualAssign(
286+
$nodeScopeResolver,
279287
$scope,
280288
$storage,
281289
$stmt,
@@ -292,7 +300,8 @@ public function applyCallScopeEffects(NodeScopeResolver $nodeScopeResolver, Stmt
292300
) {
293301
$arrayArg = $normalizedExpr->getArgs()[0]->value;
294302

295-
$scope = $nodeScopeResolver->processVirtualAssign(
303+
$scope = $this->assignHandler->processVirtualAssign(
304+
$nodeScopeResolver,
296305
$scope,
297306
$storage,
298307
$stmt,

‎src/Analyser/ExprHandler/PostDecHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
private ExpressionResultFactory $expressionResultFactory,
3535
private DefaultNarrowingHelper $defaultNarrowingHelper,
3636
private IncDecTypeHelper $incDecTypeHelper,
37+
private AssignHandler $assignHandler,
3738
)
3839
{
3940
}
@@ -70,7 +71,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
7071
// rule-side asks about it answer from the storage
7172
$nodeScopeResolver->storeExpressionResult($storage, $virtualExpr, $virtualExprResult);
7273

73-
$assignedScope = $nodeScopeResolver->processVirtualAssign(
74+
$assignedScope = $this->assignHandler->processVirtualAssign(
75+
$nodeScopeResolver,
7476
$varResult->getScope(),
7577
$storage,
7678
$stmt,

‎src/Analyser/ExprHandler/PostIncHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
private ExpressionResultFactory $expressionResultFactory,
3535
private DefaultNarrowingHelper $defaultNarrowingHelper,
3636
private IncDecTypeHelper $incDecTypeHelper,
37+
private AssignHandler $assignHandler,
3738
)
3839
{
3940
}
@@ -70,7 +71,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
7071
// rule-side asks about it answer from the storage
7172
$nodeScopeResolver->storeExpressionResult($storage, $virtualExpr, $virtualExprResult);
7273

73-
$assignedScope = $nodeScopeResolver->processVirtualAssign(
74+
$assignedScope = $this->assignHandler->processVirtualAssign(
75+
$nodeScopeResolver,
7476
$varResult->getScope(),
7577
$storage,
7678
$stmt,

‎src/Analyser/ExprHandler/PreDecHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(
3232
private ExpressionResultFactory $expressionResultFactory,
3333
private IncDecTypeHelper $incDecTypeHelper,
3434
private DefaultNarrowingHelper $defaultNarrowingHelper,
35+
private AssignHandler $assignHandler,
3536
)
3637
{
3738
}
@@ -72,7 +73,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
7273
// final result after this handler returns
7374
$nodeScopeResolver->storeExpressionResult($storage, $expr, $incDecValueResult);
7475

75-
$assignedScope = $nodeScopeResolver->processVirtualAssign(
76+
$assignedScope = $this->assignHandler->processVirtualAssign(
77+
$nodeScopeResolver,
7678
$varResult->getScope(),
7779
$storage,
7880
$stmt,

‎src/Analyser/ExprHandler/PreIncHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(
3232
private ExpressionResultFactory $expressionResultFactory,
3333
private IncDecTypeHelper $incDecTypeHelper,
3434
private DefaultNarrowingHelper $defaultNarrowingHelper,
35+
private AssignHandler $assignHandler,
3536
)
3637
{
3738
}
@@ -72,7 +73,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
7273
// final result after this handler returns
7374
$nodeScopeResolver->storeExpressionResult($storage, $expr, $incDecValueResult);
7475

75-
$assignedScope = $nodeScopeResolver->processVirtualAssign(
76+
$assignedScope = $this->assignHandler->processVirtualAssign(
77+
$nodeScopeResolver,
7678
$varResult->getScope(),
7779
$storage,
7880
$stmt,

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
use PhpParser\Node\Stmt\Static_;
2222
use PhpParser\Node\Stmt\Switch_;
2323
use PhpParser\NodeFinder;
24-
use PHPStan\Analyser\ExprHandler\AssignHandler;
2524
use PHPStan\Analyser\ExprHandler\Helper\NonNullabilityHelper;
26-
use PHPStan\Analyser\ExprHandler\Helper\VirtualExprResultHelper;
2725
use PHPStan\Analyser\Generics\TemplateArgumentConstraints;
2826
use PHPStan\Analyser\Generics\TemplateArgumentFrame;
2927
use PHPStan\Analyser\Generics\TemplateArgumentObserver;
@@ -34,8 +32,6 @@
3432
use PHPStan\DependencyInjection\Container;
3533
use PHPStan\DependencyInjection\ExtensionsCollection;
3634
use PHPStan\File\FileHelper;
37-
use PHPStan\Node\Expr\NativeTypeExpr;
38-
use PHPStan\Node\Expr\TypeExpr;
3935
use PHPStan\Node\FunctionCallableNode;
4036
use PHPStan\Node\FunctionCallExpressionNode;
4137
use PHPStan\Node\InstantiationCallableNode;
@@ -1088,60 +1084,6 @@ public function callNodeCallback(
10881084
$nodeCallback($node, $scope->toNodeCallbackScope());
10891085
}
10901086

1091-
/**
1092-
* @param callable(Node $node, Scope $scope): void $nodeCallback
1093-
*/
1094-
public function processVirtualAssign(MutatingScope $scope, ExpressionResultStorage $storage, Node\Stmt $stmt, Expr $var, Expr $assignedExpr, callable $nodeCallback, ?ExpressionResult $assignedExprResult = null): ExpressionResult
1095-
{
1096-
// work off an available result for the assigned expr: passed by the
1097-
// caller, or fabricated from a type-carrying virtual node - threaded
1098-
// straight into applyWrite() so its reads compose instead of falling
1099-
// back to on-demand pricing of the type, the truthy/falsey narrowing,
1100-
// and the synthetic sentinel comparisons
1101-
if (
1102-
$assignedExprResult === null
1103-
&& ($assignedExpr instanceof TypeExpr || $assignedExpr instanceof NativeTypeExpr)
1104-
&& $storage->findExpressionResult($assignedExpr) === null
1105-
) {
1106-
$assignedExprResult = $this->container->getByType(VirtualExprResultHelper::class)->createTypeExprResult($scope, $assignedExpr);
1107-
}
1108-
1109-
$assignHandler = $this->container->getByType(AssignHandler::class);
1110-
$virtualAssignNodeCallback = VirtualAssignNodeCallback::create($nodeCallback);
1111-
$target = $assignHandler->prepareTarget(
1112-
$this,
1113-
$scope,
1114-
$storage,
1115-
$stmt,
1116-
$var,
1117-
$assignedExpr,
1118-
$virtualAssignNodeCallback,
1119-
ExpressionContext::createDeep(),
1120-
AssignTargetWalkMode::virtualAssign(),
1121-
);
1122-
1123-
return $assignHandler->applyWrite(
1124-
$this,
1125-
$target,
1126-
$this->expressionResultFactory->create(
1127-
$target->getScope(),
1128-
beforeScope: $target->getScope(),
1129-
expr: $assignedExpr,
1130-
hasYield: false,
1131-
isAlwaysTerminating: false,
1132-
throwPoints: [],
1133-
impurePoints: [],
1134-
typeCallback: static fn () => new MixedType(),
1135-
specifyTypesCallback: SpecifiedTypes::emptySpecifyCallback(),
1136-
),
1137-
$assignedExprResult,
1138-
$stmt,
1139-
$storage,
1140-
$virtualAssignNodeCallback,
1141-
ExpressionContext::createDeep(),
1142-
);
1143-
}
1144-
11451087
/**
11461088
* The template argument frame of the body being walked while it observes
11471089
* a body that created unresolved template arguments - null otherwise, so

‎src/Analyser/StmtHandler/ForeachHandler.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPStan\Analyser\ExpressionContext;
2424
use PHPStan\Analyser\ExpressionResultStorage;
2525
use PHPStan\Analyser\ExpressionTypeHolder;
26+
use PHPStan\Analyser\ExprHandler\AssignHandler;
2627
use PHPStan\Analyser\ExprHandler\Helper\IdenticalNarrowingHelper;
2728
use PHPStan\Analyser\InternalStatementResult;
2829
use PHPStan\Analyser\InternalThrowPoint;
@@ -83,6 +84,7 @@ public function __construct(
8384
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
8485
private bool $implicitThrows,
8586
private VarAnnotationProcessor $varAnnotationProcessor,
87+
private AssignHandler $assignHandler,
8688
)
8789
{
8890
}
@@ -531,7 +533,8 @@ private function enterForeach(NodeScopeResolver $nodeScopeResolver, MutatingScop
531533
$vars[] = $keyVarName;
532534
}
533535
} else {
534-
$scope = $nodeScopeResolver->processVirtualAssign(
536+
$scope = $this->assignHandler->processVirtualAssign(
537+
$nodeScopeResolver,
535538
$scope,
536539
$storage,
537540
$stmt,
@@ -550,7 +553,8 @@ private function enterForeach(NodeScopeResolver $nodeScopeResolver, MutatingScop
550553
$scope = $scope->enterForeachKey($originalScope, $stmt->expr, $iterateeType, $nativeIterateeType, $stmt->keyVar->name);
551554
$vars[] = $stmt->keyVar->name;
552555
} elseif ($stmt->keyVar !== null) {
553-
$scope = $nodeScopeResolver->processVirtualAssign(
556+
$scope = $this->assignHandler->processVirtualAssign(
557+
$nodeScopeResolver,
554558
$scope,
555559
$storage,
556560
$stmt,

0 commit comments

Comments
 (0)