Skip to content

Commit

Permalink
Merge pull request #50 from keboola/adamvyborny-COM-1714
Browse files Browse the repository at this point in the history
Empty header
  • Loading branch information
AdamVyborny authored Nov 2, 2022
2 parents 82cc689 + 9d079e4 commit ea1e00e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ parameters:
count: 1
path: tests/CsvReadTest.php

-
message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testEmptyHeader\\(\\) has no return type specified\\.$#"
count: 1
path: tests/CsvReadTest.php

-
message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testException\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
6 changes: 5 additions & 1 deletion src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public function __construct(
$this->validateLineBreak();

rewind($this->filePointer);
$this->header = UTF8BOMHelper::detectAndRemoveBOM($this->readLine());
$header = UTF8BOMHelper::detectAndRemoveBOM($this->readLine());
if (is_array($header) && $header[0] === null) {
$header = [];
}
$this->header = $header;
$this->rewind();
}

Expand Down
2 changes: 1 addition & 1 deletion src/UTF8BOMHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UTF8BOMHelper
*/
public static function detectAndRemoveBOM($header)
{
if (!is_array($header)) {
if (!is_array($header) || empty($header) || $header[0] === null) {
return $header;
}
$utf32BigEndianBom = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF);
Expand Down
8 changes: 7 additions & 1 deletion tests/CsvReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ public function testParseMacLineEndsInField()
}


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

public function testEmptyHeader(): void
{
$csvFile = new CsvReader(__DIR__ . '/data/test-input.emptyHeader.csv', ',', '"');
self::assertEquals([], $csvFile->getHeader());
}

public function testInitInvalidFileShouldThrowException()
{
$this->expectException(Exception::class);
Expand Down
7 changes: 7 additions & 0 deletions tests/UTF8BOMHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function testDetectAndRemoveBOM($bomFile)
$this->assertSame(['id', 'name'], UTF8BOMHelper::detectAndRemoveBOM($firstLine));
}

public function testDetectAndRemoveBOMEdgeCases(): void
{
$this->assertSame([null], UTF8BOMHelper::detectAndRemoveBOM([null]));
$this->assertSame([], UTF8BOMHelper::detectAndRemoveBOM([]));
$this->assertSame(null, UTF8BOMHelper::detectAndRemoveBOM(null)); // @phpstan-ignore-line
}

public function bomProvider()
{
return [
Expand Down
2 changes: 2 additions & 0 deletions tests/data/test-input.emptyHeader.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

"ffff","fff","ffff"

0 comments on commit ea1e00e

Please sign in to comment.