Skip to content

Commit 338bd14

Browse files
authored
reuse code mirroring, extract from AbstractRector (#7712)
1 parent d145f43 commit 338bd14

File tree

5 files changed

+55
-39
lines changed

5 files changed

+55
-39
lines changed

src/BetterPhpDocParser/Comment/CommentsMerger.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,48 @@
55
namespace Rector\BetterPhpDocParser\Comment;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\InlineHTML;
9+
use PhpParser\Node\Stmt\Nop;
10+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
811
use Rector\NodeTypeResolver\Node\AttributeKey;
12+
use Rector\PhpParser\Comparing\NodeComparator;
913

10-
final class CommentsMerger
14+
final readonly class CommentsMerger
1115
{
16+
public function __construct(
17+
private NodeComparator $nodeComparator,
18+
) {
19+
}
20+
21+
public function mirrorComments(Node $newNode, Node $oldNode): void
22+
{
23+
if ($oldNode instanceof InlineHTML) {
24+
return;
25+
}
26+
27+
if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
28+
return;
29+
}
30+
31+
$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
32+
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);
33+
34+
if ($newPhpDocInfo instanceof PhpDocInfo) {
35+
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
36+
return;
37+
}
38+
39+
if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
40+
return;
41+
}
42+
}
43+
44+
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
45+
if (! $newNode instanceof Nop) {
46+
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
47+
}
48+
}
49+
1250
/**
1351
* @param Node[] $mergedNodes
1452
*/

src/DependencyInjection/LazyContainerFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\Application\ChangedNodeScopeRefresher;
1818
use Rector\Application\FileProcessor;
1919
use Rector\Application\Provider\CurrentFileProvider;
20+
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
2021
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
2122
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
2223
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
@@ -558,6 +559,7 @@ static function (AbstractRector $rector, Container $container): void {
558559
$container->get(CurrentFileProvider::class),
559560
$container->get(CreatedByRuleDecorator::class),
560561
$container->get(ChangedNodeScopeRefresher::class),
562+
$container->get(CommentsMerger::class),
561563
);
562564
}
563565
);

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public function enterNode(Node $node): ?Node
5858
}
5959

6060
if ($node instanceof Unset_) {
61-
$this->processContextInUnset($node);
61+
foreach ($node->vars as $var) {
62+
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
63+
}
64+
6265
return null;
6366
}
6467

@@ -130,13 +133,6 @@ static function (Node $subNode): null {
130133
);
131134
}
132135

133-
private function processContextInUnset(Unset_ $unset): void
134-
{
135-
foreach ($unset->vars as $var) {
136-
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
137-
}
138-
}
139-
140136
private function processContextInIf(If_|Else_|ElseIf_ $node): void
141137
{
142138
foreach ($node->stmts as $stmt) {

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/PhpVersionConditionNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function enterNode(Node $node): ?Node
2727
if (($node instanceof Ternary || $node instanceof If_) && $this->hasVersionCompareCond($node)) {
2828
if ($node instanceof Ternary) {
2929
$nodes = [$node->else];
30-
if ($node->if instanceof \PhpParser\Node) {
30+
if ($node->if instanceof Node) {
3131
$nodes[] = $node->if;
3232
}
3333
} else {

src/Rector/AbstractRector.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use PhpParser\Node\PropertyItem;
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use PhpParser\Node\Stmt\Const_;
12-
use PhpParser\Node\Stmt\InlineHTML;
1312
use PhpParser\Node\Stmt\Interface_;
14-
use PhpParser\Node\Stmt\Nop;
1513
use PhpParser\Node\Stmt\Property;
1614
use PhpParser\Node\Stmt\Trait_;
1715
use PhpParser\NodeVisitor;
@@ -21,7 +19,7 @@
2119
use PHPStan\Type\Type;
2220
use Rector\Application\ChangedNodeScopeRefresher;
2321
use Rector\Application\Provider\CurrentFileProvider;
24-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
22+
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
2523
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
2624
use Rector\Contract\Rector\HTMLAverseRectorInterface;
2725
use Rector\Contract\Rector\RectorInterface;
@@ -71,6 +69,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
7169

7270
private CurrentFileProvider $currentFileProvider;
7371

72+
private CommentsMerger $commentsMerger;
73+
7474
/**
7575
* @var array<int, Node[]>
7676
*/
@@ -89,7 +89,8 @@ public function autowire(
8989
NodeComparator $nodeComparator,
9090
CurrentFileProvider $currentFileProvider,
9191
CreatedByRuleDecorator $createdByRuleDecorator,
92-
ChangedNodeScopeRefresher $changedNodeScopeRefresher
92+
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
93+
CommentsMerger $commentsMerger
9394
): void {
9495
$this->nodeNameResolver = $nodeNameResolver;
9596
$this->nodeTypeResolver = $nodeTypeResolver;
@@ -100,6 +101,7 @@ public function autowire(
100101
$this->currentFileProvider = $currentFileProvider;
101102
$this->createdByRuleDecorator = $createdByRuleDecorator;
102103
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
104+
$this->commentsMerger = $commentsMerger;
103105
}
104106

105107
/**
@@ -189,6 +191,8 @@ final public function enterNode(Node $node): int|Node|null
189191
/**
190192
* Replacing nodes in leaveNode() method avoids infinite recursion
191193
* see"infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
194+
*
195+
* @return Node|Node[]|NodeVisitor::REMOVE_NODE|null
192196
*/
193197
final public function leaveNode(Node $node): array|int|Node|null
194198
{
@@ -268,31 +272,7 @@ protected function traverseNodesWithCallable(Node | array $nodes, callable $call
268272

269273
protected function mirrorComments(Node $newNode, Node $oldNode): void
270274
{
271-
if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
272-
return;
273-
}
274-
275-
if ($oldNode instanceof InlineHTML) {
276-
return;
277-
}
278-
279-
$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
280-
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);
281-
282-
if ($newPhpDocInfo instanceof PhpDocInfo) {
283-
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
284-
return;
285-
}
286-
287-
if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
288-
return;
289-
}
290-
}
291-
292-
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
293-
if (! $newNode instanceof Nop) {
294-
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
295-
}
275+
$this->commentsMerger->mirrorComments($newNode, $oldNode);
296276
}
297277

298278
private function decorateCurrentAndChildren(Node $node): void

0 commit comments

Comments
 (0)