From 6b3abbedb2571f4cfde4bf25b449279fb2a6b288 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 5 Jan 2022 13:05:02 +0100 Subject: [PATCH] Fix fputcsv for PHP 8.0 (#316) --- generated/filesystem.php | 34 ------------------ generator/config/specialCasesFunctions.php | 1 + lib/special_cases.php | 41 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/generated/filesystem.php b/generated/filesystem.php index 223c572a..5a39ede3 100644 --- a/generated/filesystem.php +++ b/generated/filesystem.php @@ -895,40 +895,6 @@ function fopen(string $filename, string $mode, bool $use_include_path = false, $ } -/** - * fputcsv formats a line (passed as a - * fields array) as CSV and writes it (terminated by a - * newline) to the specified file stream. - * - * @param resource $stream The file pointer must be valid, and must point to - * a file successfully opened by fopen or - * fsockopen (and not yet closed by - * fclose). - * @param array $fields An array of strings. - * @param string $separator The optional separator parameter sets the field - * delimiter (one single-byte character only). - * @param string $enclosure The optional enclosure parameter sets the field - * enclosure (one single-byte character only). - * @param string $escape The optional escape parameter sets the - * escape character (at most one single-byte character). - * An empty string ("") disables the proprietary escape mechanism. - * @param string $eol The optional eol parameter sets - * a custom End of Line sequence. - * @return int Returns the length of the written string. - * @throws FilesystemException - * - */ -function fputcsv($stream, array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int -{ - error_clear_last(); - $result = \fputcsv($stream, $fields, $separator, $enclosure, $escape, $eol); - if ($result === false) { - throw FilesystemException::createFromPhpError(); - } - return $result; -} - - /** * fread reads up to * length bytes from the file pointer diff --git a/generator/config/specialCasesFunctions.php b/generator/config/specialCasesFunctions.php index 4704eddf..7b830d2f 100644 --- a/generator/config/specialCasesFunctions.php +++ b/generator/config/specialCasesFunctions.php @@ -7,6 +7,7 @@ 'json_decode', 'apc_fetch', 'apcu_fetch', + 'fputcsv', 'preg_replace', 'openssl_encrypt', 'readdir', diff --git a/lib/special_cases.php b/lib/special_cases.php index 70d403d7..a4d1956d 100644 --- a/lib/special_cases.php +++ b/lib/special_cases.php @@ -7,6 +7,7 @@ namespace Safe; +use Safe\Exceptions\FilesystemException; use const PREG_NO_ERROR; use Safe\Exceptions\MiscException; @@ -363,3 +364,43 @@ function posix_getpgid(int $process_id): int } return $result; } + + +/** + * fputcsv formats a line (passed as a + * fields array) as CSV and writes it (terminated by a + * newline) to the specified file stream. + * + * @param resource $stream The file pointer must be valid, and must point to + * a file successfully opened by fopen or + * fsockopen (and not yet closed by + * fclose). + * @param string[] $fields An array of strings. + * @param string $separator The optional separator parameter sets the field + * delimiter (one single-byte character only). + * @param string $enclosure The optional enclosure parameter sets the field + * enclosure (one single-byte character only). + * @param string $escape The optional escape parameter sets the + * escape character (at most one single-byte character). + * An empty string ("") disables the proprietary escape mechanism. + * @param string $eol The optional eol parameter sets + * a custom End of Line sequence. + * @return int Returns the length of the written string. + * @throws FilesystemException + * + */ +function fputcsv($stream, array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int +{ + error_clear_last(); + if (PHP_VERSION_ID >= 80100) { + /** @phpstan-ignore-next-line */ + $result = \fputcsv($stream, $fields, $separator, $enclosure, $escape, $eol); + } else { + $result = \fputcsv($stream, $fields, $separator, $enclosure, $escape); + } + + if ($result === false) { + throw FilesystemException::createFromPhpError(); + } + return $result; +}