function int_cast_for_switch()

in src/legacy_fixme/coercions.php [464:490]


function int_cast_for_switch(
  mixed $value,
  ?arraykey $first_truthy = null,
)[]: int {
  if ($value is int) return $value;
  if ($value is null) return 0;

  if ($value is string) {
    $value = \HH\str_to_numeric($value) ?? 0;
    if ($value is int) return $value;
    // fallthrough
  }
  if ($value is float) {
    return Math\floor($value) === $value ? (int)$value : SWITCH_INT_SENTINEL;
  }

  if ($value === false) return 0;
  if ($value === true) return $first_truthy as ?int ?? SWITCH_INT_SENTINEL;

  if ($value is resource) return (int)$value;

  if (\is_object($value) && !($value is \ConstCollection<_>)) {
    return (int)($value as dynamic); // this will probably throw
  }

  return SWITCH_INT_SENTINEL;
}