Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions src/NestedSetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,7 @@ public function invalidateCache(): void
*/
public function appendTo(ActiveRecord $node, bool $runValidation = true, array|null $attributes = null): bool
{
$this->operation = self::OPERATION_APPEND_TO;
$this->node = $node;

$result = $this->getOwner()->save($runValidation, $attributes);

if ($result === true) {
$node->refresh();
}

return $result;
return $this->executeOperation($node, self::OPERATION_APPEND_TO, $runValidation, $attributes);
}

/**
Expand Down Expand Up @@ -633,10 +624,7 @@ public function events(): array
*/
public function insertAfter(ActiveRecord $node, bool $runValidation = true, array|null $attributes = null): bool
{
$this->operation = self::OPERATION_INSERT_AFTER;
$this->node = $node;

return $this->getOwner()->save($runValidation, $attributes);
return $this->executeOperation($node, self::OPERATION_INSERT_AFTER, $runValidation, $attributes);
}

/**
Expand Down Expand Up @@ -678,10 +666,7 @@ public function insertAfter(ActiveRecord $node, bool $runValidation = true, arra
*/
public function insertBefore(ActiveRecord $node, bool $runValidation = true, array|null $attributes = null): bool
{
$this->operation = self::OPERATION_INSERT_BEFORE;
$this->node = $node;

return $this->getOwner()->save($runValidation, $attributes);
return $this->executeOperation($node, self::OPERATION_INSERT_BEFORE, $runValidation, $attributes);
}

/**
Expand Down Expand Up @@ -864,15 +849,7 @@ public function leaves(): ActiveQuery
*/
public function makeRoot(bool $runValidation = true, array|null $attributes = null): bool
{
$this->operation = self::OPERATION_MAKE_ROOT;

$result = $this->getOwner()->save($runValidation, $attributes);

if ($result === true) {
$this->getOwner()->refresh();
}

return $result;
return $this->executeOperation(null, self::OPERATION_MAKE_ROOT, $runValidation, $attributes);
}

/**
Expand Down Expand Up @@ -1004,10 +981,7 @@ public function parents(int|null $depth = null): ActiveQuery
*/
public function prependTo(ActiveRecord $node, bool $runValidation = true, array|null $attributes = null): bool
{
$this->operation = self::OPERATION_PREPEND_TO;
$this->node = $node;

return $this->getOwner()->save($runValidation, $attributes);
return $this->executeOperation($node, self::OPERATION_PREPEND_TO, $runValidation, $attributes);
}

/**
Expand Down Expand Up @@ -1309,6 +1283,31 @@ private function executeCrossTreeMove(
$this->shiftLeftRightAttribute($this->getRightValue(), $this->getLeftValue() - $this->getRightValue() - 1);
}

/**
* @phpstan-param array<string, mixed>|null $attributes
*/
private function executeOperation(
ActiveRecord|null $targetNode,
string $operation,
bool $runValidation,
array|null $attributes = null,
): bool {
$this->operation = $operation;
$this->node = $targetNode;

$result = $this->getOwner()->save($runValidation, $attributes);

if ($operation === self::OPERATION_APPEND_TO) {
$targetNode?->refresh();
}

if ($operation === self::OPERATION_MAKE_ROOT) {
$this->getOwner()->refresh();
}

return $result;
}

private function executeSameTreeMove(NodeContext $context, mixed $currentTreeValue): void
{
$subtreeSize = $this->getRightValue() - $this->getLeftValue() + 1;
Expand All @@ -1320,7 +1319,7 @@ private function executeSameTreeMove(NodeContext $context, mixed $currentTreeVal
$adjustedLeftValue = $this->getLeftValue();
$adjustedRightValue = $this->getRightValue();

// We use >= for consistency, though equality is impossible due to the nature of the algorithm
// We use '>=' for consistency, though equality is impossible due to the nature of the algorithm
// @infection-ignore-all
if ($adjustedLeftValue >= $context->targetPositionValue) {
$adjustedLeftValue += $subtreeSize;
Expand Down