How many times a sorted array was rotated

var_dump(rotated([5, 6, 7, 1, 2, 3]));
var_dump(rotated([6, 7, 1, 2, 3, 4]));
var_dump(rotated([1, 2, 3, 4]));
var_dump(rotated([4, 5, 3, 1]));


function rotated($array) {
  $low = 0;
  $high = count($array) - 1;
  
  while (true) {

    if ($array[$low] <= $array[$high]) {
      return $low;
    }
    
    $mid = floor(($low + $high) / 2);
    
    if ($mid == $low) {
      return $high;
    }
    
    $next = ($mid + 1) % count($array);
    $prev = ($mid + count($array) - 1) % count($array);  
   
    if ($array[$next] > $array[$mid] && $array[$prev] > $array[$mid]) {
      return $mid;
    }
    
    if ($array[$low] < $array[$mid])  {
      $low = $mid+1;
    }
        
    if ($array[$mid] < $array[$high])  {
      $high = $mid-1;
    }
  }
}
    
index