Skip to content

Commit

Permalink
Tree:linkChildren parameter order swap
Browse files Browse the repository at this point in the history
to prioritize potentially more commonly used key function
  • Loading branch information
dakujem committed Feb 4, 2024
1 parent 83ed9ef commit c701715
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion tests/tree.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit c701715

Please sign in to comment.