Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function hasResult(): bool
public function current()
{
if ($this->position === null) {
$this->count = 0;
$this->advance();
}

Expand All @@ -102,10 +103,6 @@ public function next(): void
}

if ($this->isCacheDisabled || ! $this->cache->valid()) {
// Raise count during the first loop only after each iteration, so
// that it is synchronized with how many times a loop has been run.
$this->count += 1;

$this->generator->next();
$this->advance();
} else {
Expand All @@ -116,6 +113,7 @@ public function next(): void
public function key(): int
{
if ($this->position === null) {
$this->count = 0;
$this->advance();
}

Expand All @@ -138,8 +136,8 @@ public function rewind(): void
}

if ($this->position === null) {
$this->advance();
$this->count = 0;
$this->advance();
} else {
$this->position = 0;
}
Expand Down Expand Up @@ -176,6 +174,12 @@ protected function advance(): void
} else {
$this->position += 1;
}

if ($this->limit === null || $this->position < $this->limit) {
// Raise count during the first loop only after each iteration, so
// that it is synchronized with how many times a loop has been run.
$this->count += 1;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function testCountWithCacheBeforeIteration(): void
}

$this->assertSame(['a', 'b', 'c'], $result);
$this->assertSame(3, $set->count(), 'Count must not change due to iteration');
}

public function testLimitedCountWithCacheBeforeIteration(): void
Expand Down
Loading