ElggBatch constructor doesn't return false when there are no pages. You can try with:
```php
if (!$top_pages->valid()) {
    return;
}
```