From 10553ab3f0337ff1a71433c3417d7eb2a3eec1fd Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 20 Apr 2023 13:12:21 +0200 Subject: [PATCH] CloningVisitor --- src/Ast/Attribute.php | 2 ++ src/Ast/NodeVisitor/CloningVisitor.php | 20 ++++++++++++ .../Ast/NodeVisitor/CloningVisitorTest.php | 32 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 src/Ast/NodeVisitor/CloningVisitor.php create mode 100644 tests/PHPStan/Ast/NodeVisitor/CloningVisitorTest.php diff --git a/src/Ast/Attribute.php b/src/Ast/Attribute.php index 8beccb79..cd3a0a29 100644 --- a/src/Ast/Attribute.php +++ b/src/Ast/Attribute.php @@ -11,4 +11,6 @@ final class Attribute public const START_INDEX = 'startIndex'; public const END_INDEX = 'endIndex'; + public const ORIGINAL_NODE = 'originalNode'; + } diff --git a/src/Ast/NodeVisitor/CloningVisitor.php b/src/Ast/NodeVisitor/CloningVisitor.php new file mode 100644 index 00000000..7200f3af --- /dev/null +++ b/src/Ast/NodeVisitor/CloningVisitor.php @@ -0,0 +1,20 @@ +setAttribute(Attribute::ORIGINAL_NODE, $originalNode); + + return $node; + } + +} diff --git a/tests/PHPStan/Ast/NodeVisitor/CloningVisitorTest.php b/tests/PHPStan/Ast/NodeVisitor/CloningVisitorTest.php new file mode 100644 index 00000000..e63f3425 --- /dev/null +++ b/tests/PHPStan/Ast/NodeVisitor/CloningVisitorTest.php @@ -0,0 +1,32 @@ +traverse([$node]); + $this->assertCount(1, $newNodes); + $this->assertInstanceOf(NullableTypeNode::class, $newNodes[0]); + $this->assertNotSame($node, $newNodes[0]); + $this->assertSame($node, $newNodes[0]->getAttribute(Attribute::ORIGINAL_NODE)); + + $this->assertInstanceOf(IdentifierTypeNode::class, $newNodes[0]->type); + $this->assertNotSame($identifier, $newNodes[0]->type); + $this->assertSame($identifier, $newNodes[0]->type->getAttribute(Attribute::ORIGINAL_NODE)); + } + +}