From c66ac5619c292a52df85481799a1678b55ec63ae Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sun, 1 Dec 2024 05:11:54 -0800 Subject: [PATCH] Get Us Closer to Csv Not Autodetect By Default This has been requested a few times, most recently issue #4092. Because it's a breaking change, I haven't proceeded with it. But, because I have a breaking change PR #4240 already in the queue, this gives a plan for getting where we want to go (under the extremely likely assumption that most users don't deal with Csv files with Mac line endings). This PR doesn't change the current behavior, but it gets us to a state where a single-line change will be sufficient when the time comes for a new major release. --- src/PhpSpreadsheet/Reader/Csv.php | 5 ++++- tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index 33bee9cdfa..89cbfd5ad1 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -84,10 +84,13 @@ class Csv extends BaseReader */ private static $constructorCallback; + /** Will be changed to false in next major release */ + public const DEFAULT_TEST_AUTODETECT = true; + /** * Attempt autodetect line endings (deprecated after PHP8.1)? */ - private bool $testAutodetect = true; + private bool $testAutodetect = self::DEFAULT_TEST_AUTODETECT; protected bool $castFormattedNumberToNumeric = false; diff --git a/tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php b/tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php index 28ff9f30fe..f14bdeb137 100644 --- a/tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Csv/CsvLineEndingTest.php @@ -12,6 +12,8 @@ class CsvLineEndingTest extends TestCase { private string $tempFile = ''; + private static bool $alwaysFalse = false; + protected function tearDown(): void { if ($this->tempFile !== '') { @@ -32,6 +34,9 @@ public function testEndings(string $ending): void $data = ['123', '456', '789']; file_put_contents($filename, implode($ending, $data)); $reader = new Csv(); + if (Csv::DEFAULT_TEST_AUTODETECT === self::$alwaysFalse) { + $reader->setTestAutoDetect(true); + } $spreadsheet = $reader->load($filename); $sheet = $spreadsheet->getActiveSheet(); self::assertEquals($data[0], $sheet->getCell('A1')->getValue());