/** * @param Node $node * @param array $definitions */ public function replaceContents($node, array $definitions = NULL) { // remove existing children foreach ($this->m_children as $child) { if ($child instanceof self) { $child->m_parent = NULL; } } $this->m_children = []; // add children from other node foreach ($node->m_children as $child) { if ($child instanceof IToken) { if ($child instanceof self) { $newChild = new self($child->m_token, $this); $newChild->replaceContents($child, $definitions); $child = $newChild; } else { if ($definitions != NULL && $child->type() === TokenType::T_VARIABLE && isset($definitions[$child->name()])) { // replace this token/node with a new node $def = $definitions[$child->name()]; $token = $child instanceof self ? $child->m_token : $child; $child = new Node($token, $this); $child->replaceContents($def); } } } $this->m_children[] = $child; } }