Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/BetterPhpDocParser/Comment/CommentsMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,48 @@
namespace Rector\BetterPhpDocParser\Comment;

use PhpParser\Node;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Nop;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Comparing\NodeComparator;

final class CommentsMerger
final readonly class CommentsMerger
{
public function __construct(
private NodeComparator $nodeComparator,
) {
}

public function mirrorComments(Node $newNode, Node $oldNode): void
{
if ($oldNode instanceof InlineHTML) {
return;
}

if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
return;
}

$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);

if ($newPhpDocInfo instanceof PhpDocInfo) {
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
return;
}

if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
return;
}
}

$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
if (! $newNode instanceof Nop) {
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
}
}

/**
* @param Node[] $mergedNodes
*/
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\FileProcessor;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
Expand Down Expand Up @@ -558,6 +559,7 @@ static function (AbstractRector $rector, Container $container): void {
$container->get(CurrentFileProvider::class),
$container->get(CreatedByRuleDecorator::class),
$container->get(ChangedNodeScopeRefresher::class),
$container->get(CommentsMerger::class),
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function enterNode(Node $node): ?Node
}

if ($node instanceof Unset_) {
$this->processContextInUnset($node);
foreach ($node->vars as $var) {
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
}

return null;
}

Expand Down Expand Up @@ -130,13 +133,6 @@ static function (Node $subNode): null {
);
}

private function processContextInUnset(Unset_ $unset): void
{
foreach ($unset->vars as $var) {
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
}
}

private function processContextInIf(If_|Else_|ElseIf_ $node): void
{
foreach ($node->stmts as $stmt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function enterNode(Node $node): ?Node
if (($node instanceof Ternary || $node instanceof If_) && $this->hasVersionCompareCond($node)) {
if ($node instanceof Ternary) {
$nodes = [$node->else];
if ($node->if instanceof \PhpParser\Node) {
if ($node->if instanceof Node) {
$nodes[] = $node->if;
}
} else {
Expand Down
38 changes: 9 additions & 29 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeVisitor;
Expand All @@ -21,7 +19,7 @@
use PHPStan\Type\Type;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\Contract\Rector\RectorInterface;
Expand Down Expand Up @@ -71,6 +69,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter

private CurrentFileProvider $currentFileProvider;

private CommentsMerger $commentsMerger;

/**
* @var array<int, Node[]>
*/
Expand All @@ -89,7 +89,8 @@ public function autowire(
NodeComparator $nodeComparator,
CurrentFileProvider $currentFileProvider,
CreatedByRuleDecorator $createdByRuleDecorator,
ChangedNodeScopeRefresher $changedNodeScopeRefresher
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
CommentsMerger $commentsMerger
): void {
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
Expand All @@ -100,6 +101,7 @@ public function autowire(
$this->currentFileProvider = $currentFileProvider;
$this->createdByRuleDecorator = $createdByRuleDecorator;
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
$this->commentsMerger = $commentsMerger;
}

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

protected function mirrorComments(Node $newNode, Node $oldNode): void
{
if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
return;
}

if ($oldNode instanceof InlineHTML) {
return;
}

$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);

if ($newPhpDocInfo instanceof PhpDocInfo) {
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
return;
}

if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
return;
}
}

$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
if (! $newNode instanceof Nop) {
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
}
$this->commentsMerger->mirrorComments($newNode, $oldNode);
}

private function decorateCurrentAndChildren(Node $node): void
Expand Down