Skip to content

Commit 9d079e4

Browse files
committed
Set empty array as header when header is missing
1 parent 6041ee0 commit 9d079e4

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ parameters:
215215
count: 1
216216
path: tests/CsvReadTest.php
217217

218-
-
219-
message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testEmptyHeader\\(\\) has no return type specified\\.$#"
220-
count: 1
221-
path: tests/CsvReadTest.php
222-
223218
-
224219
message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testException\\(\\) has no return type specified\\.$#"
225220
count: 1

src/CsvReader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function __construct(
6262
$this->validateLineBreak();
6363

6464
rewind($this->filePointer);
65-
$this->header = UTF8BOMHelper::detectAndRemoveBOM($this->readLine());
65+
$header = UTF8BOMHelper::detectAndRemoveBOM($this->readLine());
66+
if (is_array($header) && $header[0] === null) {
67+
$header = [];
68+
}
69+
$this->header = $header;
6670
$this->rewind();
6771
}
6872

tests/CsvReadTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,18 @@ public function testParseMacLineEndsInField()
175175
}
176176

177177

178-
public function testEmptyHeader()
178+
public function testEmptyCsv(): void
179179
{
180180
$csvFile = new CsvReader(__DIR__ . '/data/test-input.empty.csv', ',', '"');
181181
self::assertEquals([], $csvFile->getHeader());
182182
}
183183

184+
public function testEmptyHeader(): void
185+
{
186+
$csvFile = new CsvReader(__DIR__ . '/data/test-input.emptyHeader.csv', ',', '"');
187+
self::assertEquals([], $csvFile->getHeader());
188+
}
189+
184190
public function testInitInvalidFileShouldThrowException()
185191
{
186192
$this->expectException(Exception::class);

tests/data/test-input.emptyHeader.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
"ffff","fff","ffff"

0 commit comments

Comments
 (0)