1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-16 03:17:02 +01:00
psalm/src/Psalm/Internal/PhpVisitor/CloningVisitor.php

37 lines
914 B
PHP
Raw Normal View History

2019-07-05 22:24:00 +02:00
<?php
declare(strict_types=1);
2020-03-15 04:54:42 +01:00
namespace Psalm\Internal\PhpVisitor;
2019-07-05 22:24:00 +02:00
use function array_map;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
/**
* Visitor cloning all nodes and linking to the original nodes using an attribute.
*
* This visitor is required to perform format-preserving pretty prints.
*/
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(
/**
* @return \PhpParser\Comment
*/
function (\PhpParser\Comment $c): \PhpParser\Comment {
2018-10-28 02:21:57 +01:00
return clone $c;
},
$cs
)
);
}
2019-07-05 22:24:00 +02:00
return $node;
}
}