Skip to content

Chunked fetch fails in case of less available mails than page size #114

@zssarkany

Description

@zssarkany

Describe the bug
If there are fewer messages available by query than the chunk_size value, callback is never called.

See related code segment of Webklex\PHPIMAP\Query\Query from current master.
https://github.com/Webklex/php-imap/blob/53dc02344979f152d6cde4ad2ad99f186d3f1ebb/src/Query/Query.php

// segment from Webklex\PHPIMAP\Query\Query
    public function chunked($callback, $chunk_size = 10, $start_chunk = 1) {
        $available_messages = $this->search();
        if (($available_messages_count = $available_messages->count()) > 0) {
            $old_limit = $this->limit;
            $old_page = $this->page;

            $this->limit = $chunk_size;
            $this->page = $start_chunk;
            while ($this->limit * $this->page <= $available_messages_count) {
                $messages = $this->populate($available_messages);
                $callback($messages, $this->page);
                $this->page++;
            }
            $this->limit = $old_limit;
            $this->page = $old_page;
        }
    }

The cause of the issue seems to be here: while ($this->limit * $this->page <= $available_messages_count)

For example there are 2 matching messages, chunk_size (10) and start_chunk (1) are defaults. In this case the while condition evaluates to false (10 * 1 <= 2), therefore callback is not invoked.

It seems, that there are two possible approach to fix it.

  • A: changing start_chunk default to 0 (weird solution)
  • B: change expression to $this->limit * ($this->page - 1) <= $available_messages_count, and implement some constraints to avoid negative values.

Any thoughts about this?
Can you confirm the issue?
Should I file a PR?

Used config
My setup utilizes default configuration of webklex/laravel-imap package.

Code to Reproduce
Issue seems to be obvious by taking a look on the code segment above.

Expected behavior
Callback should be invoked in case of just a couple of messages exist in the specific folder. (Less than chunk size value)

Screenshots
n/a

Desktop / Server (please complete the following information):
irrelevant

Additional context
Used as a dependency of webklex/laravel-imap v2.4.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions