From fe3fdaf0b23c10a8357ff1b9b0c858152e812bde Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 29 Jun 2025 11:04:08 -0400 Subject: [PATCH 1/2] refactor: Simplify node insertion logic in `beforeInsert()` method of `NestedSetsBehavior` class. --- src/NestedSetsBehavior.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index e21ff4f..9541c0f 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -363,15 +363,24 @@ public function beforeDelete(): void */ public function beforeInsert(): void { - $nodeLeftValue = $this->node?->getAttribute($this->leftAttribute) ?? 0; - $nodeRightValue = $this->node?->getAttribute($this->rightAttribute) ?? 0; - - match ($this->operation) { - self::OPERATION_APPEND_TO => $this->beforeInsertNode($nodeRightValue, 1), - self::OPERATION_INSERT_AFTER => $this->beforeInsertNode($nodeRightValue + 1, 0), - self::OPERATION_INSERT_BEFORE => $this->beforeInsertNode($nodeLeftValue, 0), - self::OPERATION_MAKE_ROOT => $this->beforeInsertRootNode(), - self::OPERATION_PREPEND_TO => $this->beforeInsertNode($nodeLeftValue + 1, 1), + match (true) { + $this->operation === self::OPERATION_APPEND_TO && $this->node !== null => $this->beforeInsertNode( + $this->node->getAttribute($this->rightAttribute), + 1, + ), + $this->operation === self::OPERATION_INSERT_AFTER && $this->node !== null => $this->beforeInsertNode( + $this->node->getAttribute($this->rightAttribute) + 1, + 0, + ), + $this->operation === self::OPERATION_INSERT_BEFORE && $this->node !== null => $this->beforeInsertNode( + $this->node->getAttribute($this->leftAttribute), + 0, + ), + $this->operation === self::OPERATION_MAKE_ROOT => $this->beforeInsertRootNode(), + $this->operation === self::OPERATION_PREPEND_TO && $this->node !== null => $this->beforeInsertNode( + $this->node->getAttribute($this->leftAttribute) + 1, + 1, + ), default => throw new NotSupportedException( 'Method "' . get_class($this->getOwner()) . '::insert" is not supported for inserting new nodes.', ), From 528c6bf35c55bf5560e06856b976547b1dc5f55d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 29 Jun 2025 15:04:29 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/NestedSetsBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index 9541c0f..be4ad4c 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -377,7 +377,7 @@ public function beforeInsert(): void 0, ), $this->operation === self::OPERATION_MAKE_ROOT => $this->beforeInsertRootNode(), - $this->operation === self::OPERATION_PREPEND_TO && $this->node !== null => $this->beforeInsertNode( + $this->operation === self::OPERATION_PREPEND_TO && $this->node !== null => $this->beforeInsertNode( $this->node->getAttribute($this->leftAttribute) + 1, 1, ),