Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 337 : Reader\Csv::inferSeparator() generates a notice when the file is empty #743

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
syntaxCheck="true"
disallowTestOutput="true">
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="2048M"/>
</php>
<testsuite name="PhpSpreadsheet Unit Test Suite">
Expand Down
7 changes: 7 additions & 0 deletions src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ protected function inferSeparator()
}
}

// If number of lines is 0, nothing to infer : fall back to the default
if ($numberLines === 0) {
$this->delimiter = reset($potentialDelimiters);

return $this->skipBOM();
}

// Calculate the mean square deviations for each delimiter (ignoring delimiters that haven't been found consistently)
$meanSquareDeviations = [];
$middleIdx = floor(($numberLines - 1) / 2);
Expand Down
12 changes: 12 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public function providerDelimiterDetection()
'D8',
-58.373161,
],
[
'data/Reader/CSV/empty.csv',
',',
'A1',
null,
],
[
'data/Reader/CSV/no_delimiter.csv',
',',
'A1',
'SingleLine',
],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/data/Reader/CSV/no_delimiter.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SingleLine