Skip to content

Commit

Permalink
Csv reader avoid notice when the file is empty
Browse files Browse the repository at this point in the history
Fixes #337
  • Loading branch information
Laurent authored and PowerKiKi committed Oct 28, 2018
1 parent 4e8e0dc commit 79d86ef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

- Support numeric condition in SUMIF, SUMIFS, AVERAGEIF, COUNTIF, MAXIF and MINIF - [#683](https://github.com/PHPOffice/PhpSpreadsheet/issues/683)
- SUMIFS containing multiple conditions - [#704](https://github.com/PHPOffice/PhpSpreadsheet/issues/704)
- Csv reader avoid notice when the file is empty - [#743](https://github.com/PHPOffice/PhpSpreadsheet/pull/743)

## [1.5.0] - 2018-10-21

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

0 comments on commit 79d86ef

Please sign in to comment.