Skip to content

Commit a9057b0

Browse files
committed
Fix wrong phpstan_cache_printer attribute for synthetic nodes
1 parent b4a517c commit a9057b0

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
use PHPStan\Node\MethodCallableNode;
120120
use PHPStan\Node\MethodReturnStatementsNode;
121121
use PHPStan\Node\NoopExpressionNode;
122+
use PHPStan\Node\Printer\ExprPrinter;
122123
use PHPStan\Node\PropertyAssignNode;
123124
use PHPStan\Node\PropertyHookReturnStatementsNode;
124125
use PHPStan\Node\PropertyHookStatementNode;
@@ -3210,13 +3211,15 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
32103211
} elseif ($expr instanceof Expr\NullsafeMethodCall) {
32113212
$beforeScope = $scope;
32123213
$nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var);
3214+
$attributes = array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true]);
3215+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
32133216
$exprResult = $this->processExprNode(
32143217
$stmt,
32153218
new MethodCall(
32163219
$expr->var,
32173220
$expr->name,
32183221
$expr->args,
3219-
array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true]),
3222+
$attributes,
32203223
),
32213224
$nonNullabilityResult->getScope(),
32223225
$storage,
@@ -3438,10 +3441,12 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
34383441
} elseif ($expr instanceof Expr\NullsafePropertyFetch) {
34393442
$beforeScope = $scope;
34403443
$nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var);
3444+
$attributes = array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true]);
3445+
unset($attributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
34413446
$exprResult = $this->processExprNode($stmt, new PropertyFetch(
34423447
$expr->var,
34433448
$expr->name,
3444-
array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true]),
3449+
$attributes,
34453450
), $nonNullabilityResult->getScope(), $storage, $nodeCallback, $context);
34463451
$scope = $this->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions());
34473452

@@ -3669,10 +3674,12 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36693674
$impurePoints = array_merge($condResult->getImpurePoints(), $rightResult->getImpurePoints());
36703675
$isAlwaysTerminating = $condResult->isAlwaysTerminating();
36713676
} elseif ($expr instanceof BinaryOp\Pipe) {
3677+
$rightAttributes = array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true]);
3678+
unset($rightAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]);
36723679
if ($expr->right instanceof FuncCall && $expr->right->isFirstClassCallable()) {
36733680
$exprResult = $this->processExprNode($stmt, new FuncCall($expr->right->name, [
36743681
new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])),
3675-
], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $storage, $nodeCallback, $context);
3682+
], $rightAttributes), $scope, $storage, $nodeCallback, $context);
36763683
$scope = $exprResult->getScope();
36773684
$hasYield = $exprResult->hasYield();
36783685
$throwPoints = $exprResult->getThrowPoints();
@@ -3681,7 +3688,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36813688
} elseif ($expr->right instanceof MethodCall && $expr->right->isFirstClassCallable()) {
36823689
$exprResult = $this->processExprNode($stmt, new MethodCall($expr->right->var, $expr->right->name, [
36833690
new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])),
3684-
], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $storage, $nodeCallback, $context);
3691+
], $rightAttributes), $scope, $storage, $nodeCallback, $context);
36853692
$scope = $exprResult->getScope();
36863693
$hasYield = $exprResult->hasYield();
36873694
$throwPoints = $exprResult->getThrowPoints();
@@ -3690,7 +3697,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36903697
} elseif ($expr->right instanceof StaticCall && $expr->right->isFirstClassCallable()) {
36913698
$exprResult = $this->processExprNode($stmt, new StaticCall($expr->right->class, $expr->right->name, [
36923699
new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])),
3693-
], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $storage, $nodeCallback, $context);
3700+
], $rightAttributes), $scope, $storage, $nodeCallback, $context);
36943701
$scope = $exprResult->getScope();
36953702
$hasYield = $exprResult->hasYield();
36963703
$throwPoints = $exprResult->getThrowPoints();
@@ -3699,7 +3706,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
36993706
} else {
37003707
$exprResult = $this->processExprNode($stmt, new FuncCall($expr->right, [
37013708
new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])),
3702-
], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $storage, $nodeCallback, $context);
3709+
], $rightAttributes), $scope, $storage, $nodeCallback, $context);
37033710
$scope = $exprResult->getScope();
37043711
$hasYield = $exprResult->hasYield();
37053712
$throwPoints = $exprResult->getThrowPoints();

src/Node/Printer/ExprPrinter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
final class ExprPrinter
1313
{
1414

15+
public const ATTRIBUTE_CACHE_KEY = 'phpstan_cache_printer';
16+
1517
public function __construct(private Printer $printer)
1618
{
1719
}
1820

1921
public function printExpr(Expr $expr): string
2022
{
2123
/** @var string|null $exprString */
22-
$exprString = $expr->getAttribute('phpstan_cache_printer');
24+
$exprString = $expr->getAttribute(self::ATTRIBUTE_CACHE_KEY);
2325
if ($exprString === null) {
2426
$exprString = $this->printer->prettyPrintExpr($expr);
25-
$expr->setAttribute('phpstan_cache_printer', $exprString);
27+
$expr->setAttribute(self::ATTRIBUTE_CACHE_KEY, $exprString);
2628
}
2729

2830
return $exprString;

0 commit comments

Comments
 (0)