From 4354a49e5b7ec6dcf0089bd709ed9df90d3d5ba2 Mon Sep 17 00:00:00 2001 From: upstream Date: Fri, 4 Sep 2026 09:31:11 +0200 Subject: [PATCH] Upstream thephpleague/csv state of the touched files before 3426bb277e --- src/Buffer.php | 13 +++++++++---- src/BufferTest.php | 18 ------------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/Buffer.php b/src/Buffer.php index 843c219..d4083c2 100644 --- a/src/Buffer.php +++ b/src/Buffer.php @@ -335,11 +335,16 @@ final class Buffer implements TabularData private function nthRow(int $nth, string $method): array { -1 < $nth || throw InvalidArgument::dueToInvalidRecordOffset($nth, $method); + if (null === ($first = $this->firstOffset())) { + return []; + } - // $nth is a positional index (like ResultSet::nth), so it must be - // resolved against the record order rather than the row keys, which - // may contain gaps after records have been deleted. - return array_values($this->rows)[$nth] ?? []; + $offset = $first + $nth; + if (!array_key_exists($offset, $this->rows)) { + return []; + } + + return $this->rows[$nth + $first]; } public function fetchColumn(int|string $index = 0): Iterator diff --git a/src/BufferTest.php b/src/BufferTest.php index 8c19af1..e373f7e 100644 --- a/src/BufferTest.php +++ b/src/BufferTest.php @@ -285,24 +285,6 @@ final class BufferTest extends TestCase self::assertSame(['date' => '2025-02-08', 'temperature' => '3', 'place' => 'Berkeley'], $buffer->nth(0)); } - #[Test] - public function it_returns_records_by_positional_offset_after_a_middle_record_is_deleted(): void - { - $buffer = new Buffer(['id', 'name']); - $buffer->insert(['1', 'a'], ['2', 'b'], ['3', 'c'], ['4', 'd'], ['5', 'e']); - - // Delete the second record (id = 2), which leaves a gap in the row keys. - self::assertSame(1, $buffer->delete(fn (array $row): bool => $row['id'] === '2')); - self::assertSame(4, $buffer->recordCount()); - - // nth() is positional and must stay consistent with getRecords(). - self::assertSame(['id' => '1', 'name' => 'a'], $buffer->nth(0)); - self::assertSame(['id' => '3', 'name' => 'c'], $buffer->nth(1)); - self::assertSame(['id' => '4', 'name' => 'd'], $buffer->nth(2)); - self::assertSame(['id' => '5', 'name' => 'e'], $buffer->nth(3)); - self::assertSame([], $buffer->nth(4)); - } - #[Test] public function it_can_not_insert_invalid_records(): void {