diff --git a/src/Tree.php b/src/Tree.php index 8f36d96..25de8f0 100644 --- a/src/Tree.php +++ b/src/Tree.php @@ -85,8 +85,8 @@ public static function unlink( public static function linkChildren( MovableNodeContract $parent, iterable $children, - ?callable $onParentUnlinked = null, ?callable $key = null, + ?callable $onParentUnlinked = null, ): MovableNodeContract { foreach ($children as $index => $child) { if (!$child instanceof MovableNodeContract) { diff --git a/tests/nodes.phpt b/tests/nodes.phpt index 87b0acc..fa0b05c 100644 --- a/tests/nodes.phpt +++ b/tests/nodes.phpt @@ -173,7 +173,7 @@ require_once __DIR__ . '/setup.php'; Assert::same($b, $e->parent()); Assert::same([$d, $e], $b->children()); Assert::same([], $d->children()); - Tree::linkChildren($d, $e, function (TreeNodeContract $originalParent) use (&$hasRun, $b) { + Tree::linkChildren($d, $e, onParentUnlinked: function (TreeNodeContract $originalParent) use (&$hasRun, $b) { $hasRun = true; // B is the original parent of E Assert::same($b, $originalParent); diff --git a/tests/tree.phpt b/tests/tree.phpt index 4c05ea3..6d4d9b9 100644 --- a/tests/tree.phpt +++ b/tests/tree.phpt @@ -52,11 +52,20 @@ require_once __DIR__ . '/setup.php'; Assert::same($one, $shouldBeOne); $counter = 0; - Tree::linkChildren($one, $childrenOfTwo, function ($parent) use (&$counter, $two) { + Tree::linkChildren($one, $childrenOfTwo, onParentUnlinked: function ($parent) use (&$counter, $two) { Assert::same($two, $parent); $counter += 1; }); Assert::same(2, $counter); + + // note: the parent has changed to "one" + Tree::linkChildren($one, $childrenOfTwo, key: fn(Node $node) => $node->data()); // key by data + Assert::same([ + 0 => $b, + 1 => $c, + 'Y' => $y, + 'Z' => $z, + ], $one->children()); })(); (function () {