@@ -445,10 +445,20 @@ public function insert($key, $value, $comparator)
445445
446446 if ($ cmp < 0 ) {
447447 if ($ this ->information & 2 ) {
448- $ leftBalance = $ this ->left ->information & ~3 ;
449- $ this ->left = $ this ->left ->insert ($ key , $ value , $ comparator );
448+ $ left = $ this ->left ;
449+
450+ if (!$ left instanceof self) {
451+ return $ node ;
452+ }
453+
454+ $ leftBalance = $ left ->information & ~3 ;
455+ $ this ->left = $ left ->insert ($ key , $ value , $ comparator );
450456
451- if (($ this ->left ->information & ~3 ) && ($ this ->left ->information & ~3 ) != $ leftBalance ) {
457+ if (
458+ $ this ->left instanceof self
459+ && ($ this ->left ->information & ~3 )
460+ && ($ this ->left ->information & ~3 ) != $ leftBalance
461+ ) {
452462 $ node = $ this ->decBalance ();
453463 }
454464 } else {
@@ -458,10 +468,20 @@ public function insert($key, $value, $comparator)
458468 }
459469 } elseif ($ cmp > 0 ) {
460470 if ($ this ->information & 1 ) {
461- $ rightBalance = $ this ->right ->information & ~3 ;
462- $ this ->right = $ this ->right ->insert ($ key , $ value , $ comparator );
471+ $ right = $ this ->right ;
472+
473+ if (!$ right instanceof self) {
474+ return $ node ;
475+ }
476+
477+ $ rightBalance = $ right ->information & ~3 ;
478+ $ this ->right = $ right ->insert ($ key , $ value , $ comparator );
463479
464- if (($ this ->right ->information & ~3 ) && ($ this ->right ->information & ~3 ) != $ rightBalance ) {
480+ if (
481+ $ this ->right instanceof self
482+ && ($ this ->right ->information & ~3 )
483+ && ($ this ->right ->information & ~3 ) != $ rightBalance
484+ ) {
465485 $ node = $ this ->incBalance ();
466486 }
467487 } else {
0 commit comments