Skip to content

Commit ea1e00e

Browse files
authored
Merge pull request #50 from keboola/adamvyborny-COM-1714
Empty header
2 parents 82cc689 + 9d079e4 commit ea1e00e

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
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

src/UTF8BOMHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class UTF8BOMHelper
1212
*/
1313
public static function detectAndRemoveBOM($header)
1414
{
15-
if (!is_array($header)) {
15+
if (!is_array($header) || empty($header) || $header[0] === null) {
1616
return $header;
1717
}
1818
$utf32BigEndianBom = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF);

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/UTF8BOMHelperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public function testDetectAndRemoveBOM($bomFile)
2323
$this->assertSame(['id', 'name'], UTF8BOMHelper::detectAndRemoveBOM($firstLine));
2424
}
2525

26+
public function testDetectAndRemoveBOMEdgeCases(): void
27+
{
28+
$this->assertSame([null], UTF8BOMHelper::detectAndRemoveBOM([null]));
29+
$this->assertSame([], UTF8BOMHelper::detectAndRemoveBOM([]));
30+
$this->assertSame(null, UTF8BOMHelper::detectAndRemoveBOM(null)); // @phpstan-ignore-line
31+
}
32+
2633
public function bomProvider()
2734
{
2835
return [

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)