From c701715f73e9fa1f9e1773fa79a2e4e1842085d0 Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Sun, 4 Feb 2024 21:05:42 +0100 Subject: [PATCH] Tree:linkChildren parameter order swap to prioritize potentially more commonly used key function --- src/Tree.php | 2 +- tests/nodes.phpt | 2 +- tests/tree.phpt | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) 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 () {