Skip to content

Refactor SplFileObject CSV methods #8317

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

Merged
merged 1 commit into from
Apr 8, 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
171 changes: 78 additions & 93 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2331,35 +2331,32 @@ PHP_METHOD(SplFileObject, fgetcsv)

CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);

switch (ZEND_NUM_ARGS()) {
case 3:
if (esc_len > 1) {
zend_argument_value_error(3, "must be empty or a single character");
if (delim) {
if (d_len != 1) {
zend_argument_value_error(1, "must be a single character");
RETURN_THROWS();
}
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
escape = (unsigned char) esc[0];
}
ZEND_FALLTHROUGH;
case 2:
delimiter = delim[0];
}
if (enclo) {
if (e_len != 1) {
zend_argument_value_error(2, "must be a single character");
RETURN_THROWS();
}
enclosure = enclo[0];
ZEND_FALLTHROUGH;
case 1:
if (d_len != 1) {
zend_argument_value_error(1, "must be a single character");
}
if (esc) {
if (esc_len > 1) {
zend_argument_value_error(3, "must be empty or a single character");
RETURN_THROWS();
}
delimiter = delim[0];
ZEND_FALLTHROUGH;
case 0:
break;
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
escape = (unsigned char) esc[0];
}
}

if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value) == FAILURE) {
RETURN_FALSE;
}
Expand All @@ -2378,49 +2375,41 @@ PHP_METHOD(SplFileObject, fputcsv)
zval *fields = NULL;
zend_string *eol = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == SUCCESS) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == FAILURE) {
RETURN_THROWS();
}

switch(ZEND_NUM_ARGS())
{
case 5:
case 4:
switch (esc_len) {
case 0:
escape = PHP_CSV_NO_ESCAPE;
break;
case 1:
escape = (unsigned char) esc[0];
break;
default:
zend_argument_value_error(4, "must be empty or a single character");
RETURN_THROWS();
}
ZEND_FALLTHROUGH;
case 3:
if (e_len != 1) {
zend_argument_value_error(3, "must be a single character");
RETURN_THROWS();
}
enclosure = enclo[0];
ZEND_FALLTHROUGH;
case 2:
if (d_len != 1) {
zend_argument_value_error(2, "must be a single character");
RETURN_THROWS();
}
delimiter = delim[0];
ZEND_FALLTHROUGH;
case 1:
case 0:
break;
if (delim) {
if (d_len != 1) {
zend_argument_value_error(2, "must be a single character");
RETURN_THROWS();
}

ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol);
if (ret < 0) {
RETURN_FALSE;
delimiter = delim[0];
}
if (enclo) {
if (e_len != 1) {
zend_argument_value_error(3, "must be a single character");
RETURN_THROWS();
}
RETURN_LONG(ret);
enclosure = enclo[0];
}
if (esc) {
if (esc_len > 1) {
zend_argument_value_error(4, "must be empty or a single character");
RETURN_THROWS();
}
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
escape = (unsigned char) esc[0];
}
}

ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol);
if (ret < 0) {
RETURN_FALSE;
}
RETURN_LONG(ret);
}
/* }}} */

Expand All @@ -2433,43 +2422,39 @@ PHP_METHOD(SplFileObject, setCsvControl)
char *delim = NULL, *enclo = NULL, *esc = NULL;
size_t d_len = 0, e_len = 0, esc_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
switch(ZEND_NUM_ARGS())
{
case 3:
switch (esc_len) {
case 0:
escape = PHP_CSV_NO_ESCAPE;
break;
case 1:
escape = (unsigned char) esc[0];
break;
default:
zend_argument_value_error(3, "must be empty or a single character");
RETURN_THROWS();
}
ZEND_FALLTHROUGH;
case 2:
if (e_len != 1) {
zend_argument_value_error(2, "must be a single character");
RETURN_THROWS();
}
enclosure = enclo[0];
ZEND_FALLTHROUGH;
case 1:
if (d_len != 1) {
zend_argument_value_error(1, "must be a single character");
RETURN_THROWS();
}
delimiter = delim[0];
ZEND_FALLTHROUGH;
case 0:
break;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) {
RETURN_THROWS();
}

if (delim) {
if (d_len != 1) {
zend_argument_value_error(1, "must be a single character");
RETURN_THROWS();
}
intern->u.file.delimiter = delimiter;
intern->u.file.enclosure = enclosure;
intern->u.file.escape = escape;
delimiter = delim[0];
}
if (enclo) {
if (e_len != 1) {
zend_argument_value_error(2, "must be a single character");
RETURN_THROWS();
}
enclosure = enclo[0];
}
if (esc) {
if (esc_len > 1) {
zend_argument_value_error(3, "must be empty or a single character");
RETURN_THROWS();
}
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
escape = (unsigned char) esc[0];
}
}

intern->u.file.delimiter = delimiter;
intern->u.file.enclosure = enclosure;
intern->u.file.escape = escape;
}
/* }}} */

Expand Down
6 changes: 6 additions & 0 deletions ext/spl/tests/SplFileObject_fputcsv_variation14.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ try {
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($fo->fputcsv(array('water', 'fruit'), ',', '""'));
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

unset($fo);

Expand All @@ -27,5 +32,6 @@ unlink($file);
?>
--EXPECT--
*** Testing fputcsv() : with enclosure & delimiter of two chars and file opened in read mode ***
SplFileObject::fputcsv(): Argument #2 ($separator) must be a single character
SplFileObject::fputcsv(): Argument #3 ($enclosure) must be a single character
Done