1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 15:09:04 +01:00
psalm/src/Psalm/Internal/PhpVisitor/CloningVisitor.php

41 lines
863 B
PHP
Raw Normal View History

2019-07-05 22:24:00 +02:00
<?php
2021-12-15 04:42:37 +01:00
2019-07-05 22:24:00 +02:00
declare(strict_types=1);
2021-12-15 04:42:37 +01:00
2020-03-15 04:54:42 +01:00
namespace Psalm\Internal\PhpVisitor;
2021-12-03 20:11:20 +01:00
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
2021-06-08 04:55:21 +02:00
use function array_map;
/**
* Visitor cloning all nodes and linking to the original nodes using an attribute.
*
* This visitor is required to perform format-preserving pretty prints.
2022-01-03 07:55:32 +01:00
*
* @internal
*/
class CloningVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): Node
{
2020-08-10 05:19:59 +02:00
$node = clone $node;
2018-10-28 02:21:57 +01:00
if ($cs = $node->getComments()) {
$node->setAttribute(
'comments',
array_map(
/**
2021-12-04 03:37:19 +01:00
* @return Comment
2018-10-28 02:21:57 +01:00
*/
2022-01-05 23:45:11 +01:00
fn(Comment $c): Comment => clone $c,
2018-10-28 02:21:57 +01:00
$cs
)
);
}
2019-07-05 22:24:00 +02:00
return $node;
}
}