Skip to content

Commit 1a81251

Browse files
committed
Use some early returns in spl_directory
1 parent a3abcc0 commit 1a81251

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

ext/spl/spl_directory.c

+47-48
Original file line numberDiff line numberDiff line change
@@ -1900,27 +1900,26 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /
19001900

19011901
static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */
19021902
{
1903-
int ret = SUCCESS;
1904-
19051903
do {
1906-
ret = spl_filesystem_file_read(intern, 1);
1907-
} while (ret == SUCCESS && !intern->u.file.current_line_len && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
1904+
int ret = spl_filesystem_file_read(intern, 1);
1905+
if (ret != SUCCESS) {
1906+
return ret;
1907+
}
1908+
} while (!intern->u.file.current_line_len && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
19081909

1909-
if (ret == SUCCESS) {
1910-
size_t buf_len = intern->u.file.current_line_len;
1911-
char *buf = estrndup(intern->u.file.current_line, buf_len);
1910+
size_t buf_len = intern->u.file.current_line_len;
1911+
char *buf = estrndup(intern->u.file.current_line, buf_len);
19121912

1913-
if (!Z_ISUNDEF(intern->u.file.current_zval)) {
1914-
zval_ptr_dtor(&intern->u.file.current_zval);
1915-
ZVAL_UNDEF(&intern->u.file.current_zval);
1916-
}
1913+
if (!Z_ISUNDEF(intern->u.file.current_zval)) {
1914+
zval_ptr_dtor(&intern->u.file.current_zval);
1915+
ZVAL_UNDEF(&intern->u.file.current_zval);
1916+
}
19171917

1918-
php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, &intern->u.file.current_zval);
1919-
if (return_value) {
1920-
ZVAL_COPY(return_value, &intern->u.file.current_zval);
1921-
}
1918+
php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, &intern->u.file.current_zval);
1919+
if (return_value) {
1920+
ZVAL_COPY(return_value, &intern->u.file.current_zval);
19221921
}
1923-
return ret;
1922+
return SUCCESS;
19241923
}
19251924
/* }}} */
19261925

@@ -2300,42 +2299,42 @@ PHP_METHOD(SplFileObject, fgetcsv)
23002299
char *delim = NULL, *enclo = NULL, *esc = NULL;
23012300
size_t d_len = 0, e_len = 0, esc_len = 0;
23022301

2303-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
2302+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) {
2303+
RETURN_THROWS();
2304+
}
23042305

2305-
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2306+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
23062307

2307-
switch(ZEND_NUM_ARGS())
2308-
{
2309-
case 3:
2310-
if (esc_len > 1) {
2311-
zend_argument_value_error(3, "must be empty or a single character");
2312-
RETURN_THROWS();
2313-
}
2314-
if (esc_len == 0) {
2315-
escape = PHP_CSV_NO_ESCAPE;
2316-
} else {
2317-
escape = (unsigned char) esc[0];
2318-
}
2319-
ZEND_FALLTHROUGH;
2320-
case 2:
2321-
if (e_len != 1) {
2322-
zend_argument_value_error(2, "must be a single character");
2323-
RETURN_THROWS();
2324-
}
2325-
enclosure = enclo[0];
2326-
ZEND_FALLTHROUGH;
2327-
case 1:
2328-
if (d_len != 1) {
2329-
zend_argument_value_error(1, "must be a single character");
2330-
RETURN_THROWS();
2331-
}
2332-
delimiter = delim[0];
2333-
ZEND_FALLTHROUGH;
2334-
case 0:
2335-
break;
2308+
switch (ZEND_NUM_ARGS()) {
2309+
case 3:
2310+
if (esc_len > 1) {
2311+
zend_argument_value_error(3, "must be empty or a single character");
2312+
RETURN_THROWS();
2313+
}
2314+
if (esc_len == 0) {
2315+
escape = PHP_CSV_NO_ESCAPE;
2316+
} else {
2317+
escape = (unsigned char) esc[0];
23362318
}
2337-
spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value);
2319+
ZEND_FALLTHROUGH;
2320+
case 2:
2321+
if (e_len != 1) {
2322+
zend_argument_value_error(2, "must be a single character");
2323+
RETURN_THROWS();
2324+
}
2325+
enclosure = enclo[0];
2326+
ZEND_FALLTHROUGH;
2327+
case 1:
2328+
if (d_len != 1) {
2329+
zend_argument_value_error(1, "must be a single character");
2330+
RETURN_THROWS();
2331+
}
2332+
delimiter = delim[0];
2333+
ZEND_FALLTHROUGH;
2334+
case 0:
2335+
break;
23382336
}
2337+
spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value);
23392338
}
23402339
/* }}} */
23412340

0 commit comments

Comments
 (0)