Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fputcsv for PHP 8.0 #316

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions generated/filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'json_decode',
'apc_fetch',
'apcu_fetch',
'fputcsv',
'preg_replace',
'openssl_encrypt',
'readdir',
Expand Down
41 changes: 41 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Safe;

use Safe\Exceptions\FilesystemException;
use const PREG_NO_ERROR;

use Safe\Exceptions\MiscException;
Expand Down Expand Up @@ -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;
}