Skip to content

Commit c98f146

Browse files
committed
Calculate the depth before calling
1 parent 694abf7 commit c98f146

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

src/Storage/DbalNestedSet.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -308,23 +308,23 @@ public function moveSubTreeToRoot(Node $node) {
308308
*/
309309
public function moveSubTreeBelow(Node $target, Node $node) {
310310
$newLeftPosition = $target->getLeft() + 1;
311-
$this->moveSubTreeToPosition($newLeftPosition, $node);
311+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
312312
}
313313

314314
/**
315315
* {@inheritdoc}
316316
*/
317317
public function moveSubTreeBefore(Node $target, Node $node) {
318318
$newLeftPosition = $target->getLeft();
319-
$this->moveSubTreeToPosition($newLeftPosition, $node);
319+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
320320
}
321321

322322
/**
323323
* {@inheritdoc}
324324
*/
325325
public function moveSubTreeAfter(Node $target, Node $node) {
326326
$newLeftPosition = $target->getRight() + 1;
327-
$this->moveSubTreeToPosition($newLeftPosition, $node);
327+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
328328
}
329329

330330
/**
@@ -334,11 +334,13 @@ public function moveSubTreeAfter(Node $target, Node $node) {
334334
* The new left position.
335335
* @param \PNX\NestedSet\Node $node
336336
* The node to move.
337+
* @param int $newDepth
338+
* Depth of new position.
337339
*
338340
* @throws \Exception
339341
* If a transaction error occurs.
340342
*/
341-
protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
343+
protected function moveSubTreeToPosition($newLeftPosition, Node $node, $newDepth) {
342344
try {
343345
// Calculate position adjustment variables.
344346
$width = $node->getRight() - $node->getLeft() + 1;
@@ -348,14 +350,7 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
348350
$this->connection->beginTransaction();
349351

350352
// Calculate depth difference.
351-
$newNode = $this->getNodeAtPosition($newLeftPosition);
352-
if (!$newNode) {
353-
// No other children at this position, new depth = 1.
354-
$depthDiff = 1 - $node->getDepth();
355-
}
356-
else {
357-
$depthDiff = $newNode->getDepth() - $node->getDepth();
358-
}
353+
$depthDiff = $newDepth - $node->getDepth();
359354

360355
// Backwards movement must account for new space.
361356
if ($distance < 0) {

tests/Functional/DbalNestedSetTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,9 @@ public function testMoveSubTreeBelowEndChildNode() {
392392
$nodeKey = new NodeKey(7, 1);
393393
$node = $this->nestedSet->getNode($nodeKey);
394394

395-
$this->printTree($this->nestedSet->getTree());
396395
$newRoot = $this->nestedSet->addRootNode(new NodeKey(12, 1));
397396
$newChild = $this->nestedSet->addNodeBelow($newRoot, new NodeKey(13, 1));
398-
$this->printTree($this->nestedSet->getTree());
399397
$this->nestedSet->moveSubTreeBelow($newChild, $node);
400-
$this->printTree($this->nestedSet->getTree());
401398

402399
// Check node is in new position.
403400
$node = $this->nestedSet->getNode($nodeKey);

0 commit comments

Comments
 (0)