Skip to content

Commit c6f407a

Browse files
committed
Allow adding the same node under the same key without having effect
1 parent 265d4be commit c6f407a

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/MovableNodeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function addChild(TreeNodeContract $child, string|int|null $key = null):
4040
$this->children[] = $child;
4141
} elseif (!isset($this->children[$key])) {
4242
$this->children[$key] = $child;
43-
} else {
43+
} elseif ($child !== $this->children[$key]) {
4444
throw (new ChildKeyCollision('Collision not allowed: ' . $key))
4545
->tag('parent', $this)
4646
->tag('child', $child)

tests/nodes.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ require_once __DIR__ . '/setup.php';
294294
$node->addChild(new Node(null), 'key');
295295
}, ChildKeyCollision::class, 'Collision not allowed: key');
296296

297-
// The current implementation does not allow this even when the same node is being added with the same key.
298-
// This is intentional, for simplicity. Tree::link covers this cases without hassle.
297+
// ... except for adding the same node with the same key, which has no effect.
298+
$node = new Node(null);
299299
$child = new Node(null);
300300
$node->addChild($child, 'another');
301-
Assert::throws(function () use ($node, $child) {
302-
$node->addChild($child, 'another');
303-
}, ChildKeyCollision::class, 'Collision not allowed: another');
301+
Assert::same(['another' => $child], $node->children());
302+
$node->addChild($child, 'another');
303+
Assert::same(['another' => $child], $node->children());
304304
})();

0 commit comments

Comments
 (0)