Please split the actual logic into a separate class, that can be tested in isolation. The maintenance script itself should only hanlde command line options and status reporting. Compare e.g. rebuildTermSearchKey.php.