src/Support/Collection/StringSet.php
/**
     * Replaces the first occurance of either item or the replacement item in
     * the set if they exist and removes all other occurances.
     */
Replaces the first occurance of either item or the replacement item in
the set if they exist and removes all other occurances.
Replaces the first occurance of either item or the replacement item in the set if they exist and removes all other occurances .
public function replace(string $item, string $newItem): void
    {
        if ($item === $newItem) {
            return;
        }
        $containsItem = isset($this->cache[$item]);
        $containsNewItem = isset($this->cache[$newItem]);
        if (!$containsItem) {
            return;
        }
        unset($this->cache[$item]);
        if ($containsNewItem) {
            --$this->length;
            if ($this->list[$this->length] === $item) {
                array_pop($this->list);
                return;
            }
            $popped = null;
            if ($this->list[$this->length] === $newItem) {
                $popped = array_pop($this->list);
            }
            $itemIndex = array_search($item, $this->list, true);
            if ($popped === null) {
                $newItemIndex = array_search($newItem, $this->list, true);
                if ($itemIndex > $newItemIndex) {
                    array_splice($this->list, $itemIndex, 1);
                    return;
                }
                array_splice($this->list, $newItemIndex, 1);
            }
            $this->list[$itemIndex] = $newItem;
            return;
        }
        $this->cache[$newItem] = true;
        if ($this->list[$this->length - 1] === $item) {
            array_pop($this->list);
            $this->list[] = $newItem;
            return;
        }
        $index = array_search($item, $this->list, true);
        $this->list[$index] = $newItem;
    }
public function replace ( string $ item , string $ newItem ) : void { if ( $ item === $ newItem ) { return ; } $ containsItem = isset ( $ this -> cache [ $ item ] ) ; $ containsNewItem = isset ( $ this -> cache [ $ newItem ] ) ; if ( ! $ containsItem ) { return ; } unset ( $ this -> cache [ $ item ] ) ; if ( $ containsNewItem ) { -- $ this -> length ; if ( $ this -> list [ $ this -> length ] === $ item ) { array_pop ( $ this -> list ) ; return ; } $ popped = null ; if ( $ this -> list [ $ this -> length ] === $ newItem ) { $ popped = array_pop ( $ this -> list ) ; } $ itemIndex = array_search ( $ item , $ this -> list , true ) ; if ( $ popped === null ) { $ newItemIndex = array_search ( $ newItem , $ this -> list , true ) ; if ( $ itemIndex > $ newItemIndex ) { array_splice ( $ this -> list , $ itemIndex , 1 ) ; return ; } array_splice ( $ this -> list , $ newItemIndex , 1 ) ; } $ this -> list [ $ itemIndex ] = $ newItem ; return ; } $ this -> cache [ $ newItem ] = true ; if ( $ this -> list [ $ this -> length - 1 ] === $ item ) { array_pop ( $ this -> list ) ; $ this -> list [ ] = $ newItem ; return ; } $ index = array_search ( $ item , $ this -> list , true ) ; $ this -> list [ $ index ] = $ newItem ; }
Replaces the first occurance of either item or the replacement item in
 the set if they exist and removes all other occurances.
Replaces the first occurance of either item or the replacement item in the set if they exist and removes all other occurances .
// If for whatever reason item is being replaced by itself, just don't // bother doing any work. // If it doesn't contain item, then there is nothing to replace or move. // We now know that the list contains item and it will ultimately be // removed, so remove it from the cache. // At this point, we know that the list contains both item and // replacement item, so we know that item is going to be removed; // decrement the length. // If item is the last item in the list just pop it off as we don't // need to do anything with replacement item since it's already in // the list and came before item. // If the replacement item is the last item in the list, pop it off // since we will be replacing item with replacement item later. // Find item's position in the list. // If the replacement item wasn't the last item in the list, we need // to find its position in the list. // If item comes after the replacement item in the list, we only // need to remove item here since the replacement item is the // first instance of the two in this case. // At this point, we know that the replacement item comes after // item in the list. Remove it from the list so that we can // insert it into item's place next. // We removed the existing instance of replacement item from the // list above and now we will replace item with replacement item. // At this point the list only contains item and not the replacement // item. Add the replacement item to the cache as it is not currently in // the list. // If item is the last item in the list, pop it off the list and append // the replacement item.