Skip to content

Commit dc8c831

Browse files
committed
Unwrap virtual nodes in AST when fixing code
1 parent acc0fd9 commit dc8c831

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/Analyser/RuleErrorTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Fixable\PhpPrinter;
1313
use PHPStan\Fixable\PhpPrinterIndentationDetectorVisitor;
1414
use PHPStan\Fixable\ReplacingNodeVisitor;
15+
use PHPStan\Fixable\UnwrapVirtualNodesVisitor;
1516
use PHPStan\Node\VirtualNode;
1617
use PHPStan\Rules\FileRuleError;
1718
use PHPStan\Rules\FixableNodeRuleError;
@@ -118,6 +119,7 @@ public function transform(
118119
$indentTraverser->traverse($fileNodes);
119120

120121
$cloningTraverser = new NodeTraverser();
122+
$cloningTraverser->addVisitor(new UnwrapVirtualNodesVisitor());
121123
$cloningTraverser->addVisitor(new CloningVisitor());
122124

123125
/** @var Stmt[] $newStmts */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Fixable;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitorAbstract;
7+
use PHPStan\Node\Expr\AlwaysRememberedExpr;
8+
9+
final class UnwrapVirtualNodesVisitor extends NodeVisitorAbstract
10+
{
11+
12+
public function enterNode(Node $node): ?Node
13+
{
14+
if (!$node instanceof Node\Expr\Match_) {
15+
return null;
16+
}
17+
18+
if (!$node->cond instanceof AlwaysRememberedExpr) {
19+
return null;
20+
}
21+
22+
$node->cond = $node->cond->expr;
23+
24+
return $node;
25+
}
26+
27+
}

tests/PHPStan/Build/NamedArgumentsRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,16 @@ public function testFix(): void
6464
);
6565
}
6666

67+
public function testFixFileWithMatch(): void
68+
{
69+
if (PHP_VERSION_ID < 80000) {
70+
$this->markTestSkipped('Test requires PHP 8.0.');
71+
}
72+
73+
$this->fix(
74+
__DIR__ . '/data/named-arguments-match.php',
75+
__DIR__ . '/data/named-arguments-match.php.fixed',
76+
);
77+
}
78+
6779
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.0
2+
3+
namespace NamedArgumentsMatchRule;
4+
5+
use Exception;
6+
7+
function (bool $a, bool $b): void {
8+
foreach ([1, 2, 3] as $v) {
9+
match (true) {
10+
$a => 1,
11+
$b => 2,
12+
default => 3,
13+
};
14+
new Exception('foo', 0, new Exception('prev'));
15+
}
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.0
2+
3+
namespace NamedArgumentsMatchRule;
4+
5+
use Exception;
6+
7+
function (bool $a, bool $b): void {
8+
foreach ([1, 2, 3] as $v) {
9+
match (true) {
10+
$a => 1,
11+
$b => 2,
12+
default => 3,
13+
};
14+
new Exception('foo', previous: new Exception('prev'));
15+
}
16+
};

0 commit comments

Comments
 (0)