src/CliMenu.php
protected function moveSelectionVertically(string $direction) : void
    {
        $itemKeys = array_keys($this->items);

        $increments = 0;

        do {
            $increments++;

            if ($increments > count($itemKeys)) {
                //full cycle detected, there must be no selected items
                //in the menu, so stop trying to select one.
                return;
            }

            $direction === 'UP'
                ? $this->selectedItem--
                : $this->selectedItem++;

            if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) {
                $this->selectedItem  = $direction === 'UP'
                    ? end($itemKeys)
                    : reset($itemKeys);
            }
        } while (!$this->canSelect());
    }
protected function moveSelectionVertically(string $direction) : void
    {
        $itemKeys = array_keys($this->items);

        $increments = 0;

        do {
            $increments++;

            if ($increments > count($itemKeys)) {
                //full cycle detected, there must be no selected items
                //in the menu, so stop trying to select one.
                return;
            }

            $direction === 'UP'
                ? $this->selectedItem--
                : $this->selectedItem++;

            if ($this->selectedItem !== null && !array_key_exists($this->selectedItem, $this->items)) {
                $this->selectedItem  = $direction === 'UP'
                    ? end($itemKeys)
                    : reset($itemKeys);
            }
        } while (!$this->canSelect());
    }
Move the selection in a given direction, up / down