Skip to content

Commit

Permalink
Php 8.4 Will Deprecate fgetcsv Parameter
Browse files Browse the repository at this point in the history
As described in issue PHPOffice#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.
  • Loading branch information
oleibman committed Sep 6, 2024
1 parent e1dae99 commit 352eaae
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ public function listWorksheetInfo(string $filename): array
$delimiter = $this->delimiter ?? '';

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

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

// Loop through each line of the file in turn
$delimiter = $this->delimiter ?? '';
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
$valueBinder = Cell::getValueBinder();
$preserveBooleanString = method_exists($valueBinder, 'getBooleanConversion') && $valueBinder->getBooleanConversion();
$this->getTrue = Calculation::getTRUE();
Expand Down Expand Up @@ -416,7 +416,7 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
}
++$columnLetter;
}
$rowData = fgetcsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
++$currentRow;
}

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

return $this;
}

/**
* Php8.4 deprecates use of anything other than null string
* as escape Character.
*
* @param resource $stream
* @param null|int<0, max> $length
*
* @return array<int,?string>|false
*/
private static function getCsv(
$stream,
?int $length = null,
string $separator = ',',
string $enclosure = '"',
string $escape = '\\'
): array|false {
if (PHP_VERSION_ID >= 80400 && $escape !== '') {
return @fgetcsv($stream, $length, $separator, $enclosure, $escape);
}

return fgetcsv($stream, $length, $separator, $enclosure, $escape);
}
}

0 comments on commit 352eaae

Please sign in to comment.