Skip to content

Commit

Permalink
Merge pull request #318 from Jean85/fix-fputcsv
Browse files Browse the repository at this point in the history
Fix fputcsv annotation
  • Loading branch information
Kharhamel authored Jan 11, 2022
2 parents 418777f + 74d8991 commit aeca670
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
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
42 changes: 42 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,44 @@ 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).
* @phpstan-param (scalar|\Stringable|null)[] $fields
* @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();
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;
}

0 comments on commit aeca670

Please sign in to comment.