Skip to content

Commit 6aaa7b6

Browse files
committed
Refactor SplFileObject CSV methods
1 parent 8922706 commit 6aaa7b6

File tree

3 files changed

+95
-93
lines changed

3 files changed

+95
-93
lines changed

ext/spl/spl_directory.c

Lines changed: 82 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,35 +2328,32 @@ PHP_METHOD(SplFileObject, fgetcsv)
23282328

23292329
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
23302330

2331-
switch (ZEND_NUM_ARGS()) {
2332-
case 3:
2333-
if (esc_len > 1) {
2334-
zend_argument_value_error(3, "must be empty or a single character");
2331+
if (delim) {
2332+
if (d_len != 1) {
2333+
zend_argument_value_error(1, "must be a single character");
23352334
RETURN_THROWS();
23362335
}
2337-
if (esc_len == 0) {
2338-
escape = PHP_CSV_NO_ESCAPE;
2339-
} else {
2340-
escape = (unsigned char) esc[0];
2341-
}
2342-
ZEND_FALLTHROUGH;
2343-
case 2:
2336+
delimiter = delim[0];
2337+
}
2338+
if (enclo) {
23442339
if (e_len != 1) {
23452340
zend_argument_value_error(2, "must be a single character");
23462341
RETURN_THROWS();
23472342
}
23482343
enclosure = enclo[0];
2349-
ZEND_FALLTHROUGH;
2350-
case 1:
2351-
if (d_len != 1) {
2352-
zend_argument_value_error(1, "must be a single character");
2344+
}
2345+
if (esc) {
2346+
if (esc_len > 1) {
2347+
zend_argument_value_error(3, "must be empty or a single character");
23532348
RETURN_THROWS();
23542349
}
2355-
delimiter = delim[0];
2356-
ZEND_FALLTHROUGH;
2357-
case 0:
2358-
break;
2350+
if (esc_len == 0) {
2351+
escape = PHP_CSV_NO_ESCAPE;
2352+
} else {
2353+
escape = (unsigned char) esc[0];
2354+
}
23592355
}
2356+
23602357
if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value) == FAILURE) {
23612358
RETURN_FALSE;
23622359
}
@@ -2375,49 +2372,41 @@ PHP_METHOD(SplFileObject, fputcsv)
23752372
zval *fields = NULL;
23762373
zend_string *eol = NULL;
23772374

2378-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == SUCCESS) {
2375+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == FAILURE) {
2376+
RETURN_THROWS();
2377+
}
23792378

2380-
switch(ZEND_NUM_ARGS())
2381-
{
2382-
case 5:
2383-
case 4:
2384-
switch (esc_len) {
2385-
case 0:
2386-
escape = PHP_CSV_NO_ESCAPE;
2387-
break;
2388-
case 1:
2389-
escape = (unsigned char) esc[0];
2390-
break;
2391-
default:
2392-
zend_argument_value_error(4, "must be empty or a single character");
2393-
RETURN_THROWS();
2394-
}
2395-
ZEND_FALLTHROUGH;
2396-
case 3:
2397-
if (e_len != 1) {
2398-
zend_argument_value_error(3, "must be a single character");
2399-
RETURN_THROWS();
2400-
}
2401-
enclosure = enclo[0];
2402-
ZEND_FALLTHROUGH;
2403-
case 2:
2404-
if (d_len != 1) {
2405-
zend_argument_value_error(2, "must be a single character");
2406-
RETURN_THROWS();
2407-
}
2408-
delimiter = delim[0];
2409-
ZEND_FALLTHROUGH;
2410-
case 1:
2411-
case 0:
2412-
break;
2379+
if (delim) {
2380+
if (d_len != 1) {
2381+
zend_argument_value_error(2, "must be a single character");
2382+
RETURN_THROWS();
24132383
}
2414-
2415-
ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol);
2416-
if (ret < 0) {
2417-
RETURN_FALSE;
2384+
delimiter = delim[0];
2385+
}
2386+
if (enclo) {
2387+
if (e_len != 1) {
2388+
zend_argument_value_error(3, "must be a single character");
2389+
RETURN_THROWS();
2390+
}
2391+
enclosure = enclo[0];
2392+
}
2393+
if (esc) {
2394+
if (esc_len > 1) {
2395+
zend_argument_value_error(4, "must be empty or a single character");
2396+
RETURN_THROWS();
2397+
}
2398+
if (esc_len == 0) {
2399+
escape = PHP_CSV_NO_ESCAPE;
2400+
} else {
2401+
escape = (unsigned char) esc[0];
24182402
}
2419-
RETURN_LONG(ret);
24202403
}
2404+
2405+
ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol);
2406+
if (ret < 0) {
2407+
RETURN_FALSE;
2408+
}
2409+
RETURN_LONG(ret);
24212410
}
24222411
/* }}} */
24232412

@@ -2430,43 +2419,39 @@ PHP_METHOD(SplFileObject, setCsvControl)
24302419
char *delim = NULL, *enclo = NULL, *esc = NULL;
24312420
size_t d_len = 0, e_len = 0, esc_len = 0;
24322421

2433-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
2434-
switch(ZEND_NUM_ARGS())
2435-
{
2436-
case 3:
2437-
switch (esc_len) {
2438-
case 0:
2439-
escape = PHP_CSV_NO_ESCAPE;
2440-
break;
2441-
case 1:
2442-
escape = (unsigned char) esc[0];
2443-
break;
2444-
default:
2445-
zend_argument_value_error(3, "must be empty or a single character");
2446-
RETURN_THROWS();
2447-
}
2448-
ZEND_FALLTHROUGH;
2449-
case 2:
2450-
if (e_len != 1) {
2451-
zend_argument_value_error(2, "must be a single character");
2452-
RETURN_THROWS();
2453-
}
2454-
enclosure = enclo[0];
2455-
ZEND_FALLTHROUGH;
2456-
case 1:
2457-
if (d_len != 1) {
2458-
zend_argument_value_error(1, "must be a single character");
2459-
RETURN_THROWS();
2460-
}
2461-
delimiter = delim[0];
2462-
ZEND_FALLTHROUGH;
2463-
case 0:
2464-
break;
2422+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) {
2423+
RETURN_THROWS();
2424+
}
2425+
2426+
if (delim) {
2427+
if (d_len != 1) {
2428+
zend_argument_value_error(1, "must be a single character");
2429+
RETURN_THROWS();
2430+
}
2431+
delimiter = delim[0];
2432+
}
2433+
if (enclo) {
2434+
if (e_len != 1) {
2435+
zend_argument_value_error(2, "must be a single character");
2436+
RETURN_THROWS();
2437+
}
2438+
enclosure = enclo[0];
2439+
}
2440+
if (esc) {
2441+
if (esc_len > 1) {
2442+
zend_argument_value_error(3, "must be empty or a single character");
2443+
RETURN_THROWS();
2444+
}
2445+
if (esc_len == 0) {
2446+
escape = PHP_CSV_NO_ESCAPE;
2447+
} else {
2448+
escape = (unsigned char) esc[0];
24652449
}
2466-
intern->u.file.delimiter = delimiter;
2467-
intern->u.file.enclosure = enclosure;
2468-
intern->u.file.escape = escape;
24692450
}
2451+
2452+
intern->u.file.delimiter = delimiter;
2453+
intern->u.file.enclosure = enclosure;
2454+
intern->u.file.escape = escape;
24702455
}
24712456
/* }}} */
24722457

@@ -2476,6 +2461,10 @@ PHP_METHOD(SplFileObject, getCsvControl)
24762461
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
24772462
char delimiter[2], enclosure[2], escape[2];
24782463

2464+
if (zend_parse_parameters_none() == FAILURE) {
2465+
RETURN_THROWS();
2466+
}
2467+
24792468
array_init(return_value);
24802469

24812470
delimiter[0] = intern->u.file.delimiter;

ext/spl/tests/SplFileObject_fputcsv_variation14.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ try {
1515
} catch (ValueError $e) {
1616
echo $e->getMessage(), "\n";
1717
}
18+
try {
19+
var_dump($fo->fputcsv(array('water', 'fruit'), ',', '""'));
20+
} catch (ValueError $e) {
21+
echo $e->getMessage(), "\n";
22+
}
1823

1924
unset($fo);
2025

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

ext/spl/tests/SplFileObject_getCsvControl_basic_001.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ SplFileObject::getCsvControl function - basic test
55
$obj = New SplFileObject(__DIR__.'/SplFileObject_testinput.csv');
66
var_dump($obj->getCsvControl());
77

8+
try {
9+
var_dump($obj->getCsvControl('Args'));
10+
} catch (\TypeError) {
11+
echo "ZPP Implemented";
12+
}
13+
814
?>
915
--EXPECT--
1016
array(3) {
@@ -15,3 +21,4 @@ array(3) {
1521
[2]=>
1622
string(1) "\"
1723
}
24+
ZPP Implemented

0 commit comments

Comments
 (0)