@@ -236,19 +236,26 @@ public function isChild($id)
236236 public function replaceChild ($ childId , AbstractNode $ newChild )
237237 {
238238 $ oldChild = $ this ->children [$ childId ];
239+
239240 $ newChild ->prev = $ oldChild ['prev ' ];
240241 $ newChild ->next = $ oldChild ['next ' ];
241- unset($ oldChild ['node ' ]);
242- $ oldChild ['node ' ] = $ newChild ;
243- unset($ this ->children [$ childId ]);
244- $ this ->children [$ newChild ->id ()] = $ oldChild ;
245242
246- if ($ newChild ->prev && isset ($ this ->children [$ newChild ->prev ])) {
247- $ this ->children [$ newChild ->prev ]['next ' ] = $ newChild ->id ();
243+ $ keys = array_keys ($ this ->children );
244+ $ index = array_search ($ childId , $ keys , true );
245+ $ keys [$ index ] = $ newChild ->id ();
246+ $ this ->children = array_combine ($ keys , $ this ->children );
247+ $ this ->children [$ newChild ->id ()] = array (
248+ 'prev ' => $ oldChild ['prev ' ],
249+ 'node ' => $ newChild ,
250+ 'next ' => $ oldChild ['next ' ]
251+ );
252+
253+ if ($ oldChild ['prev ' ] && isset ($ this ->children [$ newChild ->prev ])) {
254+ $ this ->children [$ oldChild ['prev ' ]]['next ' ] = $ newChild ->id ();
248255 }
249256
250- if ($ newChild -> next && isset ($ this ->children [$ newChild ->next ])) {
251- $ this ->children [$ newChild -> next ]['prev ' ] = $ newChild ->id ();
257+ if ($ oldChild [ ' next ' ] && isset ($ this ->children [$ newChild ->next ])) {
258+ $ this ->children [$ oldChild [ ' next ' ] ]['prev ' ] = $ newChild ->id ();
252259 }
253260 }
254261
0 commit comments