Skip to content

Commit 352eaae

Browse files
committed
Php 8.4 Will Deprecate fgetcsv Parameter
As described in issue #4161, Php seems to be prepared to break the fgetcsv function in release 9, marking the existing usage deprecated in 8.4. This gives us a long-term problem. This PR provides a short-term solution.
1 parent e1dae99 commit 352eaae

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public function listWorksheetInfo(string $filename): array
228228
$delimiter = $this->delimiter ?? '';
229229

230230
// Loop through each line of the file in turn
231-
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
231+
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
232232
while (is_array($rowData)) {
233233
++$worksheetInfo[0]['totalRows'];
234234
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
235-
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
235+
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
236236
}
237237

238238
$worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1);
@@ -379,7 +379,7 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
379379

380380
// Loop through each line of the file in turn
381381
$delimiter = $this->delimiter ?? '';
382-
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
382+
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
383383
$valueBinder = Cell::getValueBinder();
384384
$preserveBooleanString = method_exists($valueBinder, 'getBooleanConversion') && $valueBinder->getBooleanConversion();
385385
$this->getTrue = Calculation::getTRUE();
@@ -416,7 +416,7 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
416416
}
417417
++$columnLetter;
418418
}
419-
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
419+
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
420420
++$currentRow;
421421
}
422422

@@ -649,4 +649,27 @@ public function setSheetNameIsFileName(bool $sheetNameIsFileName): self
649649

650650
return $this;
651651
}
652+
653+
/**
654+
* Php8.4 deprecates use of anything other than null string
655+
* as escape Character.
656+
*
657+
* @param resource $stream
658+
* @param null|int<0, max> $length
659+
*
660+
* @return array<int,?string>|false
661+
*/
662+
private static function getCsv(
663+
$stream,
664+
?int $length = null,
665+
string $separator = ',',
666+
string $enclosure = '"',
667+
string $escape = '\\'
668+
): array|false {
669+
if (PHP_VERSION_ID >= 80400 && $escape !== '') {
670+
return @fgetcsv($stream, $length, $separator, $enclosure, $escape);
671+
}
672+
673+
return fgetcsv($stream, $length, $separator, $enclosure, $escape);
674+
}
652675
}

0 commit comments

Comments
 (0)