Skip to content
Closed
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
9 changes: 9 additions & 0 deletions lib/PhpParser/NodeVisitor/NodeConnectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public function beforeTraverse(array $nodes) {
}

public function enterNode(Node $node) {
// Reconnecting a modified tree must not retain links from its old topology.
$attributes = $node->getAttributes();
if ($this->weakReferences) {
unset($attributes['weak_parent'], $attributes['weak_previous'], $attributes['weak_next']);
} else {
unset($attributes['parent'], $attributes['previous'], $attributes['next']);
}
$node->setAttributes($attributes);

if (!empty($this->stack)) {
$parent = $this->stack[count($this->stack) - 1];
if ($this->weakReferences) {
Expand Down
60 changes: 60 additions & 0 deletions test/PhpParser/NodeVisitor/NodeConnectingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,66 @@ public function testConnectsNodeToItsParentNodeAndItsSiblingNodes(): void {
$this->assertSame(Else_::class, get_class($node->getAttribute('next')));
}

/** @dataProvider provideReferenceModes */
public function testReconnectsReorderedNodes(bool $weakReferences): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php echo 1; echo 2; echo 3;'
);
$traverser = new NodeTraverser(new NodeConnectingVisitor($weakReferences));
$traverser->traverse($ast);
$ast = [$ast[2], $ast[1], $ast[0]];
$traverser->traverse($ast);
$prefix = $weakReferences ? 'weak_' : '';
$this->assertNull($ast[0]->getAttribute($prefix . 'previous'));
$this->assertNull($ast[2]->getAttribute($prefix . 'next'));
$previous = $ast[1]->getAttribute($prefix . 'previous');
$next = $ast[1]->getAttribute($prefix . 'next');
$this->assertSame($ast[0], $weakReferences ? $previous->get() : $previous);
$this->assertSame($ast[2], $weakReferences ? $next->get() : $next);
}

/** @dataProvider provideReferenceModes */
public function testReconnectsPromotedNode(bool $weakReferences): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php if (true) { echo 1; echo 2; }'
);
$traverser = new NodeTraverser(new NodeConnectingVisitor($weakReferences));
$traverser->traverse($ast);
$this->assertInstanceOf(If_::class, $ast[0]);
$node = $ast[0]->stmts[0];
$attributes = $node->getAttributes();
$traverser->traverse([$node]);
$prefix = $weakReferences ? 'weak_' : '';
$this->assertNull($node->getAttribute($prefix . 'parent'));
$this->assertNull($node->getAttribute($prefix . 'previous'));
$this->assertNull($node->getAttribute($prefix . 'next'));
$this->assertSame($attributes['startLine'], $node->getStartLine());
}

/** @dataProvider provideReferenceModes */
public function testReconnectsAfterRemovingSiblings(bool $weakReferences): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php echo 1; echo 2; echo 3;'
);
$traverser = new NodeTraverser(new NodeConnectingVisitor($weakReferences));
$traverser->traverse($ast);
$node = $ast[1];
$node->setAttribute('custom', 'preserved');
$otherAttribute = $weakReferences ? 'next' : 'weak_next';
$node->setAttribute($otherAttribute, 'independent-mode');
$traverser->traverse([$node]);
$prefix = $weakReferences ? 'weak_' : '';
$this->assertFalse($node->hasAttribute($prefix . 'previous'));
$this->assertFalse($node->hasAttribute($prefix . 'next'));
$this->assertSame('preserved', $node->getAttribute('custom'));
$this->assertSame('independent-mode', $node->getAttribute($otherAttribute));
}

/** @return array<array{bool}> */
public function provideReferenceModes(): array {
return [[false], [true]];
}

public function testWeakReferences(): void {
$ast = (new ParserFactory())->createForNewestSupportedVersion()->parse(
'<?php if (true) {} else {}'
Expand Down