Skip to content

Commit bb7a659

Browse files
committed
re-use CommentsMerger service to decouple logic from abstract rector
1 parent 1ed854b commit bb7a659

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

src/BetterPhpDocParser/Comment/CommentsMerger.php

Lines changed: 38 additions & 0 deletions
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

1014
final class CommentsMerger
1115
{
16+
public function __construct(
17+
private readonly 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\ArrayDimFetch;
1212
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1313
use PhpParser\Node\Expr\Closure;
14+
use PhpParser\Node\Expr\Isset_;
1415
use PhpParser\Node\Expr\PostDec;
1516
use PhpParser\Node\Expr\PostInc;
1617
use PhpParser\Node\Expr\PreDec;
@@ -61,13 +62,15 @@ public function enterNode(Node $node): ?Node
6162
foreach ($node->vars as $var) {
6263
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
6364
}
65+
6466
return null;
6567
}
6668

67-
if ($node instanceof Node\Expr\Isset_) {
69+
if ($node instanceof Isset_) {
6870
foreach ($node->vars as $var) {
6971
$var->setAttribute(AttributeKey::IS_ISSET_VAR, true);
7072
}
73+
7174
return null;
7275
}
7376

src/Rector/AbstractRector.php

Lines changed: 8 additions & 30 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\NodeTraverser;
@@ -22,7 +20,7 @@
2220
use PHPStan\Type\Type;
2321
use Rector\Application\ChangedNodeScopeRefresher;
2422
use Rector\Application\Provider\CurrentFileProvider;
25-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
23+
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
2624
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
2725
use Rector\Contract\Rector\HTMLAverseRectorInterface;
2826
use Rector\Contract\Rector\RectorInterface;
@@ -72,6 +70,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
7270

7371
private CurrentFileProvider $currentFileProvider;
7472

73+
private CommentsMerger $commentsMerger;
74+
7575
/**
7676
* @var array<int, Node[]>
7777
*/
@@ -90,7 +90,8 @@ public function autowire(
9090
NodeComparator $nodeComparator,
9191
CurrentFileProvider $currentFileProvider,
9292
CreatedByRuleDecorator $createdByRuleDecorator,
93-
ChangedNodeScopeRefresher $changedNodeScopeRefresher
93+
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
94+
CommentsMerger $commentsMerger
9495
): void {
9596
$this->nodeNameResolver = $nodeNameResolver;
9697
$this->nodeTypeResolver = $nodeTypeResolver;
@@ -101,6 +102,7 @@ public function autowire(
101102
$this->currentFileProvider = $currentFileProvider;
102103
$this->createdByRuleDecorator = $createdByRuleDecorator;
103104
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
105+
$this->commentsMerger = $commentsMerger;
104106
}
105107

106108
/**
@@ -183,7 +185,7 @@ final public function enterNode(Node $node): int|Node|null
183185
* Replacing nodes in leaveNode() method avoids infinite recursion
184186
* see"infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
185187
*
186-
* @return \PhpParser\Node|\PhpParser\Node[]|NodeVisitor::REMOVE_NODE|null
188+
* @return Node|Node[]|NodeVisitor::REMOVE_NODE|null
187189
*/
188190
final public function leaveNode(Node $node): array|int|Node|null
189191
{
@@ -263,31 +265,7 @@ protected function traverseNodesWithCallable(Node | array $nodes, callable $call
263265

264266
protected function mirrorComments(Node $newNode, Node $oldNode): void
265267
{
266-
if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
267-
return;
268-
}
269-
270-
if ($oldNode instanceof InlineHTML) {
271-
return;
272-
}
273-
274-
$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
275-
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);
276-
277-
if ($newPhpDocInfo instanceof PhpDocInfo) {
278-
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
279-
return;
280-
}
281-
282-
if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
283-
return;
284-
}
285-
}
286-
287-
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
288-
if (! $newNode instanceof Nop) {
289-
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
290-
}
268+
$this->commentsMerger->mirrorComments($newNode, $oldNode);
291269
}
292270

293271
/**

0 commit comments

Comments
 (0)