Skip to content

Commit 61e0f1a

Browse files
authored
Query - Chunked - Resolved infinite loop when start chunk > 1 (Webklex#477)
Resolved an issue when the start chunks value was > 1 an infinite loop would be created as handled messages would always be less than available messages when chunks are skipped. Added more safety around input arguments, forcing minimums of 1 for both $chunk_size and $start_chunk.
1 parent e5e8eb0 commit 61e0f1a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Query/Query.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,14 @@ public function get(): MessageCollection {
395395
* @throws ResponseException
396396
*/
397397
public function chunked(callable $callback, int $chunk_size = 10, int $start_chunk = 1): void {
398+
$start_chunk = max($start_chunk,1);
399+
$chunk_size = max($chunk_size,1);
400+
$skipped_messages_count = $chunk_size * ($start_chunk-1);
401+
398402
$available_messages = $this->search();
399-
if (($available_messages_count = $available_messages->count()) > 0) {
403+
$available_messages_count = max($available_messages->count() - $skipped_messages_count,0);
404+
405+
if ($available_messages_count > 0) {
400406
$old_limit = $this->limit;
401407
$old_page = $this->page;
402408

0 commit comments

Comments
 (0)