Skip to content

Commit e89e9c8

Browse files
ondrejmirtesclaude
andcommitted
Do not let a node built from another node's attributes inherit its printed key
ExprPrinter caches an expression's printed key in the node's phpstan_cache_printer attribute. Every node constructed from another node's attributes - the BooleanAnd/BooleanOr and callable virtual nodes, the single-subject Isset_ nodes of a multi-variable isset(), the plain MethodCall read for an argument-less nullsafe call, the __invoke() call of an invokable callee, the clone-with property fetches, the foreach value assignment and the $this property fetches ClassStatementsGatherer records - copied that attribute along, and so printed as the node it was made from whenever that node had been printed first. A BooleanAndNode then was the original `$foo && $i++`, and what was tracked for it answered for the original expression: its keep-void type read mixed instead of bool. Each of these now drops the cached key from the copied attributes, the way ArgumentsNormalizer and the nullsafe and pipe handlers already did, in the PHP classes and in the native mirrors. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QSGnoYacTfJv3taAhZkj5o
1 parent e8d7b79 commit e89e9c8

20 files changed

Lines changed: 179 additions & 27 deletions

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use PHPStan\DependencyInjection\ExtensionsCollection;
4646
use PHPStan\Node\ClosureReturnStatementsNode;
4747
use PHPStan\Node\Expr\TypeExpr;
48+
use PHPStan\Node\Printer\ExprPrinter;
4849
use PHPStan\Parser\ImmediatelyInvokedClosureVisitor;
4950
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
5051
use PHPStan\Reflection\Callables\SimpleImpurePoint;
@@ -232,6 +233,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
232233
foreach ($clonePropertiesArgTypeConstantArray->getKeyTypes() as $i => $clonePropertyKeyType) {
233234
$clonePropertyKeyTypeScalars = $clonePropertyKeyType->getConstantScalarValues();
234235
$propertyAttributes = $normalizedExpr->getAttributes();
236+
unset($propertyAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
235237
$propertyAttributes['inCloneWith'] = true;
236238
if (count($clonePropertyKeyTypeScalars) === 1) {
237239
$this->assignHandler->processVirtualAssign(
@@ -485,9 +487,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
485487
&& $nameType->isCallable()->yes()
486488
&& (new ObjectType(Closure::class))->isSuperTypeOf($nameType)->no()
487489
) {
490+
$invokeAttributes = $normalizedExpr->getAttributes();
491+
unset($invokeAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
488492
$invokeResult = $nodeScopeResolver->processExprNode(
489493
$stmt,
490-
new MethodCall($normalizedExpr->name, '__invoke', $normalizedExpr->getArgs(), $normalizedExpr->getAttributes()),
494+
new MethodCall($normalizedExpr->name, '__invoke', $normalizedExpr->getArgs(), $invokeAttributes),
491495
$scope,
492496
$storage,
493497
new NoopNodeCallback(),

‎src/Analyser/ExprHandler/IssetHandler.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PHPStan\DependencyInjection\AutowiredService;
2828
use PHPStan\Node\Expr\TypeExpr;
2929
use PHPStan\Node\IssetExpressionNode;
30+
use PHPStan\Node\Printer\ExprPrinter;
3031
use PHPStan\Turbo\ShadowedByTurboExtension;
3132
use PHPStan\Type\BooleanType;
3233
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -192,10 +193,14 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
192193
// subjects through the conjunction narrowing; the fabricated
193194
// Isset_/BooleanAnd nodes are only printed into holder keys,
194195
// never walked
195-
$makeSubjectTypes = fn (Expr $var, ExpressionResult $varResult): Closure => function (MutatingScope $scope, TypeSpecifierContext $ctx) use ($chainResults, $expr, $var, $varResult): SpecifiedTypes {
196+
// the single-subject isset nodes built below must not inherit the
197+
// whole isset's printed expression key
198+
$subjectAttributes = $expr->getAttributes();
199+
unset($subjectAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
200+
$makeSubjectTypes = fn (Expr $var, ExpressionResult $varResult): Closure => function (MutatingScope $scope, TypeSpecifierContext $ctx) use ($chainResults, $expr, $var, $varResult, $subjectAttributes): SpecifiedTypes {
196201
$scopedReadType = $this->defaultNarrowingHelper->buildChainTypeReader($chainResults, $scope);
197202
if ($ctx->null()) {
198-
return $this->defaultNarrowingHelper->specifyDefaultTypes(new Isset_([$var], $expr->getAttributes()), $ctx);
203+
return $this->defaultNarrowingHelper->specifyDefaultTypes(new Isset_([$var], $subjectAttributes), $ctx);
199204
}
200205
if (!$ctx->true()) {
201206
return $this->defaultNarrowingHelper->createIssetSingleSubjectNonTrueTypes($scope, $var, $varResult, $scopedReadType, $ctx, $expr);
@@ -211,13 +216,13 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
211216
return $foldAccTypes($evaluationScope, $context)->setRootExpr($expr);
212217
}
213218

214-
$accExpr = new Isset_([$expr->vars[0]], $expr->getAttributes());
219+
$accExpr = new Isset_([$expr->vars[0]], $subjectAttributes);
215220
$accTypes = $makeSubjectTypes($expr->vars[0], $varResults[0]);
216221
$accTruthyScope = $afterScope->applySpecifiedTypes($accTypes($afterScope, TypeSpecifierContext::createTruthy()));
217222
$accFalseyScope = $afterScope->applySpecifiedTypes($accTypes($afterScope, TypeSpecifierContext::createFalsey()));
218223

219224
for ($i = 1, $varCount = count($expr->vars); $i < $varCount; $i++) {
220-
$rightExprNode = new Isset_([$expr->vars[$i]], $expr->getAttributes());
225+
$rightExprNode = new Isset_([$expr->vars[$i]], $subjectAttributes);
221226
$rightTypes = $makeSubjectTypes($expr->vars[$i], $varResults[$i]);
222227
$rightFalseyScope = $accTruthyScope->applySpecifiedTypes($rightTypes($accTruthyScope, TypeSpecifierContext::createFalsey()));
223228

‎src/Analyser/MutatingScope.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3620,7 +3620,9 @@ private function resolveScopeStateType(Expr $expr, bool $native): Type
36203620
&& !$expr->isFirstClassCallable()
36213621
&& $expr->getArgs() === []
36223622
) {
3623-
return TypeCombinator::addNull($this->resolveScopeStateType(new Expr\MethodCall($expr->var, $expr->name, attributes: $expr->getAttributes()), $native));
3623+
$attributes = $expr->getAttributes();
3624+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
3625+
return TypeCombinator::addNull($this->resolveScopeStateType(new Expr\MethodCall($expr->var, $expr->name, attributes: $attributes), $native));
36243626
}
36253627

36263628
// an argument-less instance call - the shape @phpstan-assert subjects

‎src/Analyser/StmtHandler/ForeachHandler.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use PHPStan\Node\Expr\OriginalForeachKeyExpr;
4848
use PHPStan\Node\Expr\OriginalForeachValueExpr;
4949
use PHPStan\Node\InForeachNode;
50+
use PHPStan\Node\Printer\ExprPrinter;
5051
use PHPStan\Node\Variable\VariableWrite;
5152
use PHPStan\Node\VariableAssignNode;
5253
use PHPStan\TrinaryLogic;
@@ -146,7 +147,9 @@ public function processStmt(
146147
$originalScope->getIterableValueType($foreachIterateeType),
147148
$originalScope->getIterableValueType($foreachNativeIterateeType),
148149
));
149-
$virtualAssign->setAttributes($stmt->valueVar->getAttributes());
150+
$valueVarAttributes = $stmt->valueVar->getAttributes();
151+
unset($valueVarAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
152+
$virtualAssign->setAttributes($valueVarAttributes);
150153
$nodeScopeResolver->callNodeCallback($nodeCallback, $virtualAssign, $scope, $storage);
151154
}
152155

‎src/Node/BooleanAndNode.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
88
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\Node\Printer\ExprPrinter;
1011
use PHPStan\Turbo\ReferencedByTurboExtension;
1112

1213
/**
@@ -18,7 +19,10 @@ final class BooleanAndNode extends Expr implements VirtualNode
1819

1920
public function __construct(private BooleanAnd|LogicalAnd $originalNode, private Scope $rightScope)
2021
{
21-
parent::__construct($originalNode->getAttributes());
22+
// the original's printed expression key must not become this node's
23+
$attributes = $originalNode->getAttributes();
24+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
25+
parent::__construct($attributes);
2226
}
2327

2428
public function getOriginalNode(): BooleanAnd|LogicalAnd

‎src/Node/BooleanOrNode.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
88
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\Node\Printer\ExprPrinter;
1011
use PHPStan\Turbo\ReferencedByTurboExtension;
1112

1213
/**
@@ -18,7 +19,10 @@ final class BooleanOrNode extends Expr implements VirtualNode
1819

1920
public function __construct(private BooleanOr|LogicalOr $originalNode, private Scope $rightScope)
2021
{
21-
parent::__construct($originalNode->getAttributes());
22+
// the original's printed expression key must not become this node's
23+
$attributes = $originalNode->getAttributes();
24+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
25+
parent::__construct($attributes);
2226
}
2327

2428
public function getOriginalNode(): BooleanOr|LogicalOr

‎src/Node/ClassStatementsGatherer.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Node\Constant\ClassConstantFetch;
1616
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
1717
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
18+
use PHPStan\Node\Printer\ExprPrinter;
1819
use PHPStan\Node\Property\PropertyAssign;
1920
use PHPStan\Node\Property\PropertyRead;
2021
use PHPStan\Node\Property\PropertyWrite;
@@ -335,8 +336,10 @@ private function tryToApplyPromotedParameterRead(Expr\Variable $node, Scope $sco
335336
if ($param->var->name !== $variableName) {
336337
continue;
337338
}
339+
$attributes = $node->getAttributes();
340+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
338341
$this->propertyUsages[] = new PropertyRead(
339-
new PropertyFetch(new Expr\Variable('this'), new Identifier($variableName), $node->getAttributes()),
342+
new PropertyFetch(new Expr\Variable('this'), new Identifier($variableName), $attributes),
340343
$scope,
341344
);
342345
return;
@@ -362,8 +365,10 @@ private function tryToApplyPropertyWritesFromAncestorConstructor(StaticCall $anc
362365
if ($property->getName() === '') {
363366
throw new ShouldNotHappenException();
364367
}
368+
$attributes = $ancestorConstructorCall->getAttributes();
369+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
365370
$this->propertyUsages[] = new PropertyWrite(
366-
new PropertyFetch(new Expr\Variable('this'), new Identifier($property->getName()), $ancestorConstructorCall->getAttributes()),
371+
new PropertyFetch(new Expr\Variable('this'), new Identifier($property->getName()), $attributes),
367372
$scope,
368373
false,
369374
);

‎src/Node/FunctionCallableNode.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Name;
8+
use PHPStan\Node\Printer\ExprPrinter;
89
use PHPStan\Turbo\ReferencedByTurboExtension;
910

1011
/**
@@ -16,7 +17,10 @@ final class FunctionCallableNode extends Expr implements VirtualNode
1617

1718
public function __construct(private Name|Expr $name, private Expr\FuncCall $originalNode)
1819
{
19-
parent::__construct($this->originalNode->getAttributes());
20+
// the original's printed expression key must not become this node's
21+
$attributes = $this->originalNode->getAttributes();
22+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
23+
parent::__construct($attributes);
2024
}
2125

2226
/**

‎src/Node/InstantiationCallableNode.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Name;
8+
use PHPStan\Node\Printer\ExprPrinter;
89
use PHPStan\Turbo\ReferencedByTurboExtension;
910

1011
/**
@@ -16,7 +17,10 @@ final class InstantiationCallableNode extends Expr implements VirtualNode
1617

1718
public function __construct(private Name|Expr $class, private Expr\New_ $originalNode)
1819
{
19-
parent::__construct($this->originalNode->getAttributes());
20+
// the original's printed expression key must not become this node's
21+
$attributes = $this->originalNode->getAttributes();
22+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
23+
parent::__construct($attributes);
2024
}
2125

2226
/**

‎src/Node/MethodCallableNode.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Override;
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Identifier;
8+
use PHPStan\Node\Printer\ExprPrinter;
89
use PHPStan\Turbo\ReferencedByTurboExtension;
910

1011
/**
@@ -20,7 +21,10 @@ public function __construct(
2021
private Expr\MethodCall $originalNode,
2122
)
2223
{
23-
parent::__construct($originalNode->getAttributes());
24+
// the original's printed expression key must not become this node's
25+
$attributes = $originalNode->getAttributes();
26+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
27+
parent::__construct($attributes);
2428
}
2529

2630
public function getVar(): Expr

0 commit comments

Comments
 (0)