/**
* Removes the child with id $childId and replace it with the new child
* $newChild.
*
* @param int $childId
* @param AbstractNode $newChild
* @throws ChildNotFoundException
*/
public function replaceChild($childId, AbstractNode $newChild)
{
$oldChild = $this->getChild($childId);
$keys = array_keys($this->children);
$index = array_search($childId, $keys, true);
$keys[$index] = $newChild->id();
$this->children = array_combine($keys, $this->children);
$this->children[$newChild->id()] = $newChild;
unset($oldChild);
}
This method has some conceptional errors (or how to call that?). The biggest mistake is, to set directly the node in the children array. In other parts the structure of children is follow:
$children = [
hash => [
'prev' => hash,
'next' => hash,
'node' => HtmlNode,
],
[...]
]
When we look in the tests, its clear, that this coding mistake can not be detected.
$this->getChild($childId); // indirect check if child exitsts
$this->children[$childId]['node'] = $newChild;
/**
* Attempts to get the previous child.
*
* @param int $id
* @return AbstractNode
* @uses $this->getChild()
* @throws ChildNotFoundException
*/
public function previousChild($id)
{
$child = $this->getchild($id); // typo here: should be $this->getChild()
$next = $this->children[$child->id()]['prev'];
return $this->getChild($next);
}