Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ext-simplexml": "*",
"infection/infection": "^0.27|^0.29",
"maglnet/composer-require-checker": "^4.1",
"php-forge/support": "^0.1",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-strict-rules": "^2.0.3",
"phpunit/phpunit": "^10.2",
Expand Down
66 changes: 45 additions & 21 deletions src/NestedSetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,35 @@
*/
public string $depthAttribute = 'depth';

/**
* Stores the depth value for the current operation.
*/
protected int|null $depthValue = null;

/**
* Name of the attribute that stores the left boundary value of the node in the nested set tree.
*
* @phpstan-var 'lft' attribute name.
*/
public string $leftAttribute = 'lft';

/**
* Stores the left value for the current operation.
*/
protected int|null $leftValue = null;

/**
* Name of the attribute that stores the right boundary value of the node in the nested set tree.
*
* @phpstan-var 'rgt' attribute name.
*/
public string $rightAttribute = 'rgt';

/**
* Stores the right value for the current operation.
*/
protected int|null $rightValue = null;

/**
* Name of the attribute that stores the tree identifier for supporting multiple trees.
*/
Expand All @@ -130,21 +145,6 @@
*/
private Connection|null $db = null;

/**
* Stores the depth value for the current operation.
*/
private int|null $depthValue = null;

/**
* Stores the left value for the current operation.
*/
private int|null $leftValue = null;

/**
* Stores the right value for the current operation.
*/
private int|null $rightValue = null;

/**
* Handles post-deletion updates for the nested set structure.
*
Expand Down Expand Up @@ -193,9 +193,7 @@
}

$this->shiftLeftRightAttribute($this->getRightValue(), $deltaValue);

$this->operation = null;
$this->node = null;
$this->invalidateCache();
}

/**
Expand Down Expand Up @@ -235,8 +233,7 @@
);
}

$this->operation = null;
$this->node = null;
$this->invalidateCache();
}

/**
Expand Down Expand Up @@ -268,17 +265,44 @@

if ($this->operation === self::OPERATION_MAKE_ROOT) {
$this->moveNodeAsRoot($currentOwnerTreeValue);
$this->invalidateCache();

return;
}

if ($this->node === null) {
$this->invalidateCache();

return;
}

$context = $this->createMoveContext($this->node, $this->operation);

$this->moveNode($context);
$this->invalidateCache();
}

/**
* Invalidates cached attribute values and resets internal state.
*
* Clears the cached depth, left, and right attribute values, forcing them to be re-fetched from the owner model
* on next access.
*
* This method should be called after operations that modify the owner model's attributes to ensure that cached
* values remain consistent with the actual model state.
*
* Usage example:
* ```php
* // After modifying the model's attributes externally
* $behavior->invalidateCache();
* ```
*/
public function invalidateCache(): void
{
$this->depthValue = null;
$this->leftValue = null;
$this->node = null;
$this->operation = null;
$this->rightValue = null;
}

/**
Expand Down Expand Up @@ -689,7 +713,7 @@
$nodeLeft = $node->getAttribute($this->leftAttribute);
$nodeRight = $node->getAttribute($this->rightAttribute);

if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() >= $nodeRight) {

Check warning on line 716 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "GreaterThanOrEqualTo": @@ @@ { $nodeLeft = $node->getAttribute($this->leftAttribute); $nodeRight = $node->getAttribute($this->rightAttribute); - if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() >= $nodeRight) { + if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() > $nodeRight) { return false; } if ($this->treeAttribute !== false) {

Check warning on line 716 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LessThanOrEqualTo": @@ @@ { $nodeLeft = $node->getAttribute($this->leftAttribute); $nodeRight = $node->getAttribute($this->rightAttribute); - if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() >= $nodeRight) { + if ($this->getLeftValue() < $nodeLeft || $this->getRightValue() >= $nodeRight) { return false; } if ($this->treeAttribute !== false) {

Check warning on line 716 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "GreaterThanOrEqualTo": @@ @@ { $nodeLeft = $node->getAttribute($this->leftAttribute); $nodeRight = $node->getAttribute($this->rightAttribute); - if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() >= $nodeRight) { + if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() > $nodeRight) { return false; } if ($this->treeAttribute !== false) {

Check warning on line 716 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LessThanOrEqualTo": @@ @@ { $nodeLeft = $node->getAttribute($this->leftAttribute); $nodeRight = $node->getAttribute($this->rightAttribute); - if ($this->getLeftValue() <= $nodeLeft || $this->getRightValue() >= $nodeRight) { + if ($this->getLeftValue() < $nodeLeft || $this->getRightValue() >= $nodeRight) { return false; } if ($this->treeAttribute !== false) {
return false;
}

Expand Down Expand Up @@ -787,7 +811,7 @@
$this->getRightValue(),
);

return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]);

Check warning on line 814 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ public function leaves(): ActiveQuery { $condition = QueryConditionBuilder::createLeavesCondition($this->leftAttribute, $this->rightAttribute, $this->treeAttribute, $this->getTreeValue($this->getOwner()), $this->getLeftValue(), $this->getRightValue()); - return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]); + return $this->getOwner()::find()->andWhere($condition)->addOrderBy([]); } /** * Creates the root node if the active record is new, or moves it as the root node in the nested set tree.

Check warning on line 814 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ public function leaves(): ActiveQuery { $condition = QueryConditionBuilder::createLeavesCondition($this->leftAttribute, $this->rightAttribute, $this->treeAttribute, $this->getTreeValue($this->getOwner()), $this->getLeftValue(), $this->getRightValue()); - return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]); + return $this->getOwner()::find()->andWhere($condition)->addOrderBy([]); } /** * Creates the root node if the active record is new, or moves it as the root node in the nested set tree.
}

/**
Expand Down Expand Up @@ -927,7 +951,7 @@
$depth,
);

return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]);

Check warning on line 954 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ public function parents(int|null $depth = null): ActiveQuery { $condition = QueryConditionBuilder::createParentsCondition($this->leftAttribute, $this->getLeftValue(), $this->rightAttribute, $this->getRightValue(), $this->treeAttribute, $this->getTreeValue($this->getOwner()), $depth !== null ? $this->depthAttribute : null, $depth !== null ? $this->getDepthValue() : null, $depth); - return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]); + return $this->getOwner()::find()->andWhere($condition)->addOrderBy([]); } /** * Inserts the current node as the first child of the specified target node or moves it if it already exists.

Check warning on line 954 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ public function parents(int|null $depth = null): ActiveQuery { $condition = QueryConditionBuilder::createParentsCondition($this->leftAttribute, $this->getLeftValue(), $this->rightAttribute, $this->getRightValue(), $this->treeAttribute, $this->getTreeValue($this->getOwner()), $depth !== null ? $this->depthAttribute : null, $depth !== null ? $this->getDepthValue() : null, $depth); - return $this->getOwner()::find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]); + return $this->getOwner()::find()->andWhere($condition)->addOrderBy([]); } /** * Inserts the current node as the first child of the specified target node or moves it if it already exists.
}

/**
Expand Down Expand Up @@ -1106,7 +1130,7 @@
$this->getTreeValue($this->getOwner()),
);
$result = $this->getOwner()::deleteAll($condition);
$this->getOwner()->setOldAttributes(null);

Check warning on line 1133 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ } $condition = QueryConditionBuilder::createRangeCondition($this->leftAttribute, $this->getLeftValue(), $this->rightAttribute, $this->getRightValue(), $this->treeAttribute, $this->getTreeValue($this->getOwner())); $result = $this->getOwner()::deleteAll($condition); - $this->getOwner()->setOldAttributes(null); + $this->getOwner()->afterDelete(); return $result; }

Check warning on line 1133 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ } $condition = QueryConditionBuilder::createRangeCondition($this->leftAttribute, $this->getLeftValue(), $this->rightAttribute, $this->getRightValue(), $this->treeAttribute, $this->getTreeValue($this->getOwner())); $result = $this->getOwner()::deleteAll($condition); - $this->getOwner()->setOldAttributes(null); + $this->getOwner()->afterDelete(); return $result; }
$this->getOwner()->afterDelete();

return $result;
Expand Down Expand Up @@ -1134,7 +1158,7 @@

$this->shiftLeftRightAttribute($context->targetPositionValue, $subtreeSize);

if ($ownerLeftValue >= $context->targetPositionValue) {

Check warning on line 1161 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "GreaterThanOrEqualTo": @@ @@ $ownerRightValue = $this->getRightValue(); $subtreeSize = $ownerRightValue - $ownerLeftValue + 1; $this->shiftLeftRightAttribute($context->targetPositionValue, $subtreeSize); - if ($ownerLeftValue >= $context->targetPositionValue) { + if ($ownerLeftValue > $context->targetPositionValue) { $ownerLeftValue += $subtreeSize; $ownerRightValue += $subtreeSize; }

Check warning on line 1161 in src/NestedSetsBehavior.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "GreaterThanOrEqualTo": @@ @@ $ownerRightValue = $this->getRightValue(); $subtreeSize = $ownerRightValue - $ownerLeftValue + 1; $this->shiftLeftRightAttribute($context->targetPositionValue, $subtreeSize); - if ($ownerLeftValue >= $context->targetPositionValue) { + if ($ownerLeftValue > $context->targetPositionValue) { $ownerLeftValue += $subtreeSize; $ownerRightValue += $subtreeSize; }
$ownerLeftValue += $subtreeSize;
$ownerRightValue += $subtreeSize;
}
Expand Down
Loading