Fixed infinite loop when an element gets assigned itself as parent

This fixes issue #230
This commit is contained in:
Jan Böhmer 2023-03-02 22:55:22 +01:00
parent d1b8a36b93
commit 7394a23a83
5 changed files with 32 additions and 4 deletions

View file

@ -282,6 +282,12 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement
*/
public function getSubelements(): iterable
{
//If the parent is equal to this object, we would get an endless loop, so just return an empty array
//This is just a workaround, as validator should prevent this behaviour, before it gets written to the database
if ($this->parent === $this) {
return new ArrayCollection();
}
return $this->children ?? new ArrayCollection();
}

View file

@ -27,6 +27,8 @@ use InvalidArgumentException;
abstract class AbstractPartsContainingRepository extends StructuralDBElementRepository implements PartsContainingRepositoryInterface
{
private const RECURSION_LIMIT = 50;
/**
* Returns all parts associated with this element.
*
@ -55,8 +57,17 @@ abstract class AbstractPartsContainingRepository extends StructuralDBElementRepo
{
$count = $this->getPartsCount($element);
//If the element is its own parent, we have a loop in the tree, so we stop here.
if ($element->getParent() === $element) {
return 0;
}
$n = 0;
foreach ($element->getChildren() as $child) {
$count += $this->getPartsCountRecursive($child);
if ($n++ > self::RECURSION_LIMIT) {
throw new \RuntimeException('Recursion limit reached!');
}
}
return $count;