$rows The day's open rows in order. * @return int The index to insert the new task at. */ public static function insertionIndex(array $rows, int $rank, ?string $dueOn): int { foreach ($rows as $index => $row) { if ($row['isOrder']) { continue; } if (self::before($rank, $dueOn, $row['rank'], $row['dueOn'])) { return $index; } } return count($rows); } private static function before(int $rank, ?string $dueOn, int $otherRank, ?string $otherDueOn): bool { if ($rank !== $otherRank) { return $rank > $otherRank; } if ($dueOn === null) { return false; } return $otherDueOn === null || $dueOn < $otherDueOn; } }