From 79d86ef5cc73a1dbbcd3b55a31b013e506000236 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 26 Oct 2018 19:14:45 +0200 Subject: [PATCH] Csv reader avoid notice when the file is empty Fixes #337 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Csv.php | 7 +++++++ tests/PhpSpreadsheetTests/Reader/CsvTest.php | 12 ++++++++++++ tests/data/Reader/CSV/no_delimiter.csv | 1 + 4 files changed, 21 insertions(+) create mode 100644 tests/data/Reader/CSV/no_delimiter.csv diff --git a/CHANGELOG.md b/CHANGELOG.md index 474e75d8..c3f6cb5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index 0e18bcc3..25f244c1 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -181,6 +181,13 @@ class Csv extends BaseReader } } + // 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); diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php index e748cd1b..80186a41 100644 --- a/tests/PhpSpreadsheetTests/Reader/CsvTest.php +++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php @@ -67,6 +67,18 @@ class CsvTest extends TestCase 'D8', -58.373161, ], + [ + 'data/Reader/CSV/empty.csv', + ',', + 'A1', + null, + ], + [ + 'data/Reader/CSV/no_delimiter.csv', + ',', + 'A1', + 'SingleLine', + ], ]; } diff --git a/tests/data/Reader/CSV/no_delimiter.csv b/tests/data/Reader/CSV/no_delimiter.csv new file mode 100644 index 00000000..ae7c596b --- /dev/null +++ b/tests/data/Reader/CSV/no_delimiter.csv @@ -0,0 +1 @@ +SingleLine \ No newline at end of file