@@ -235,13 +235,25 @@ public function isChild($id)
235235 */
236236 public function replaceChild ($ childId , AbstractNode $ newChild )
237237 {
238- $ oldChild = $ this ->getChild ($ childId );
239- $ keys = array_keys ($ this ->children );
240- $ index = array_search ($ childId , $ keys , true );
241- $ keys [$ index ] = $ newChild ->id ();
242- $ this ->children = array_combine ($ keys , $ this ->children );
243- $ this ->children [$ newChild ->id ()] = $ newChild ;
244- unset($ oldChild );
238+ // Replace key of old child
239+ $ keys = array_keys ($ this ->children );
240+ $ index = array_search ($ childId , $ keys , true );
241+ $ keys [$ index ] = $ newChild ->id ();
242+ $ this ->children = array_combine ($ keys , $ this ->children );
243+
244+ // Replace old child node with new one
245+ $ this ->children [$ newChild ->id ()]['node ' ] = $ newChild ;
246+
247+ $ child = $ this ->children [$ newChild ->id ()];
248+
249+ // Update previous and next nodes
250+ if ($ child ['prev ' ] !== null ) {
251+ $ this ->children [$ child ['prev ' ]]['next ' ] = $ newChild ->id ();
252+ }
253+
254+ if ($ child ['next ' ] !== null ) {
255+ $ this ->children [$ child ['next ' ]]['prev ' ] = $ newChild ->id ();
256+ }
245257 }
246258
247259 /**
@@ -314,4 +326,4 @@ public function setParent(InnerNode $parent)
314326
315327 return parent ::setParent ($ parent );
316328 }
317- }
329+ }
0 commit comments