Skip to content

Commit 8922706

Browse files
committed
Minor refactoring of spl_directory.c
Using more appropriate types Fix comments Early returns
1 parent 6828f9d commit 8922706

File tree

1 file changed

+56
-58
lines changed

1 file changed

+56
-58
lines changed

ext/spl/spl_directory.c

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_
209209
return intern->path ? ZSTR_VAL(intern->path) : NULL;
210210
} /* }}} */
211211

212-
static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
212+
static inline zend_result spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
213213
{
214214
if (intern->file_name) {
215215
/* already known */
@@ -248,7 +248,8 @@ static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *int
248248
return SUCCESS;
249249
} /* }}} */
250250

251-
static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
251+
/* TODO Make void or have callers check return value */
252+
static bool spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
252253
{
253254
if (intern->file_name) {
254255
/* invalidate */
@@ -266,7 +267,7 @@ static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
266267

267268
#define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
268269

269-
static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
270+
static inline bool spl_filesystem_is_dot(const char * d_name) /* {{{ */
270271
{
271272
return !strcmp(d_name, ".") || !strcmp(d_name, "..");
272273
}
@@ -277,7 +278,7 @@ static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
277278
* Can emit an E_WARNING as it reports errors from php_stream_opendir() */
278279
static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *path)
279280
{
280-
int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
281+
bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
281282

282283
intern->type = SPL_FS_DIR;
283284
intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
@@ -375,7 +376,6 @@ static zend_object *spl_filesystem_object_clone(zend_object *old_object)
375376
zend_object *new_object;
376377
spl_filesystem_object *intern;
377378
spl_filesystem_object *source;
378-
int index, skip_dots;
379379

380380
source = spl_filesystem_from_obj(old_object);
381381
new_object = spl_filesystem_object_new_ex(old_object->ce);
@@ -392,17 +392,19 @@ static zend_object *spl_filesystem_object_clone(zend_object *old_object)
392392
intern->file_name = zend_string_copy(source->file_name);
393393
}
394394
break;
395-
case SPL_FS_DIR:
395+
case SPL_FS_DIR: {
396396
spl_filesystem_dir_open(intern, source->path);
397397
/* read until we hit the position in which we were before */
398-
skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
399-
for(index = 0; index < source->u.dir.index; ++index) {
398+
bool skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
399+
int index;
400+
for (index = 0; index < source->u.dir.index; ++index) {
400401
do {
401402
spl_filesystem_dir_read(intern);
402403
} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
403404
}
404405
intern->u.dir.index = index;
405406
break;
407+
}
406408
case SPL_FS_FILE:
407409
ZEND_UNREACHABLE();
408410
}
@@ -503,7 +505,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
503505
intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
504506
RETVAL_OBJ(&intern->std);
505507

506-
if (spl_filesystem_object_get_file_name(source) != SUCCESS) {
508+
if (spl_filesystem_object_get_file_name(source) == FAILURE) {
507509
return NULL;
508510
}
509511

@@ -540,7 +542,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
540542
intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
541543
RETVAL_OBJ(&intern->std);
542544

543-
if (spl_filesystem_object_get_file_name(source) != SUCCESS) {
545+
if (spl_filesystem_object_get_file_name(source) == FAILURE) {
544546
return NULL;
545547
}
546548

@@ -584,7 +586,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
584586
return NULL;
585587
} /* }}} */
586588

587-
static int spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
589+
static bool spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
588590
{
589591
return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
590592
}
@@ -709,7 +711,7 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long cto
709711
{
710712
spl_filesystem_object *intern;
711713
zend_string *path;
712-
int parsed;
714+
zend_result parsed;
713715
zend_long flags = (ctor_flags & ~DIT_CTOR_FLAGS);
714716
zend_error_handling error_handling;
715717

@@ -809,7 +811,7 @@ PHP_METHOD(DirectoryIterator, current)
809811
PHP_METHOD(DirectoryIterator, next)
810812
{
811813
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
812-
int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
814+
bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
813815

814816
if (zend_parse_parameters_none() == FAILURE) {
815817
RETURN_THROWS();
@@ -845,7 +847,7 @@ PHP_METHOD(DirectoryIterator, seek)
845847
}
846848

847849
while (intern->u.dir.index < pos) {
848-
int valid = 0;
850+
bool valid = 0;
849851
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
850852
valid = zend_is_true(&retval);
851853
zval_ptr_dtor(&retval);
@@ -1048,7 +1050,7 @@ PHP_METHOD(DirectoryIterator, getBasename)
10481050
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
10491051
fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
10501052

1051-
RETVAL_STR(fname);
1053+
RETURN_STR(fname);
10521054
}
10531055
/* }}} */
10541056

@@ -1082,7 +1084,7 @@ PHP_METHOD(FilesystemIterator, key)
10821084
if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
10831085
RETURN_STRING(intern->u.dir.entry.d_name);
10841086
} else {
1085-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1087+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
10861088
RETURN_THROWS();
10871089
}
10881090
RETURN_STR_COPY(intern->file_name);
@@ -1100,12 +1102,12 @@ PHP_METHOD(FilesystemIterator, current)
11001102
}
11011103

11021104
if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1103-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1105+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
11041106
RETURN_THROWS();
11051107
}
11061108
RETURN_STR_COPY(intern->file_name);
11071109
} else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1108-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1110+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
11091111
RETURN_THROWS();
11101112
}
11111113
spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
@@ -1129,7 +1131,7 @@ PHP_METHOD(DirectoryIterator, isDot)
11291131
}
11301132
/* }}} */
11311133

1132-
/* {{{ Cronstructs a new SplFileInfo from a path. */
1134+
/* {{{ Constructs a new SplFileInfo from a path. */
11331135
/* When the constructor gets called the object is already created
11341136
by the engine, so we must only call 'additional' initializations.
11351137
*/
@@ -1159,7 +1161,7 @@ PHP_METHOD(SplFileInfo, func_name) \
11591161
if (zend_parse_parameters_none() == FAILURE) { \
11601162
RETURN_THROWS(); \
11611163
} \
1162-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) { \
1164+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) { \
11631165
RETURN_THROWS(); \
11641166
} \
11651167
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
@@ -1240,7 +1242,7 @@ PHP_METHOD(SplFileInfo, getLinkTarget)
12401242
}
12411243

12421244
if (intern->file_name == NULL) {
1243-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1245+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
12441246
RETURN_THROWS();
12451247
}
12461248
}
@@ -1287,7 +1289,7 @@ PHP_METHOD(SplFileInfo, getRealPath)
12871289
}
12881290

12891291
if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
1290-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1292+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
12911293
RETURN_THROWS();
12921294
}
12931295
}
@@ -1302,12 +1304,12 @@ PHP_METHOD(SplFileInfo, getRealPath)
13021304
if (filename && VCWD_REALPATH(filename, buff)) {
13031305
#ifdef ZTS
13041306
if (VCWD_ACCESS(buff, F_OK)) {
1305-
RETVAL_FALSE;
1307+
RETURN_FALSE;
13061308
} else
13071309
#endif
1308-
RETVAL_STRING(buff);
1310+
RETURN_STRING(buff);
13091311
} else {
1310-
RETVAL_FALSE;
1312+
RETURN_FALSE;
13111313
}
13121314
}
13131315
/* }}} */
@@ -1388,7 +1390,7 @@ PHP_METHOD(SplFileInfo, getPathInfo)
13881390
PHP_METHOD(SplFileInfo, __debugInfo)
13891391
{
13901392
if (zend_parse_parameters_none() == FAILURE) {
1391-
return;
1393+
RETURN_THROWS();
13921394
}
13931395

13941396
RETURN_ARR(spl_filesystem_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
@@ -1401,7 +1403,7 @@ PHP_METHOD(SplFileInfo, _bad_state_ex)
14011403
}
14021404
/* }}} */
14031405

1404-
/* {{{ Cronstructs a new dir iterator from a path. */
1406+
/* {{{ Constructs a new dir iterator from a path. */
14051407
PHP_METHOD(FilesystemIterator, __construct)
14061408
{
14071409
spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
@@ -1412,7 +1414,7 @@ PHP_METHOD(FilesystemIterator, __construct)
14121414
PHP_METHOD(FilesystemIterator, rewind)
14131415
{
14141416
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
1415-
int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
1417+
bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
14161418

14171419
if (zend_parse_parameters_none() == FAILURE) {
14181420
RETURN_THROWS();
@@ -1468,7 +1470,7 @@ PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
14681470
if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
14691471
RETURN_FALSE;
14701472
} else {
1471-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1473+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
14721474
RETURN_THROWS();
14731475
}
14741476
php_stat(intern->file_name, FS_LPERMS, return_value);
@@ -1499,7 +1501,7 @@ PHP_METHOD(RecursiveDirectoryIterator, getChildren)
14991501
RETURN_THROWS();
15001502
}
15011503

1502-
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1504+
if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
15031505
RETURN_THROWS();
15041506
}
15051507

@@ -1720,15 +1722,15 @@ static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
17201722

17211723
if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
17221724
if (Z_ISUNDEF(iterator->current)) {
1723-
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1725+
if (spl_filesystem_object_get_file_name(object) == FAILURE) {
17241726
return NULL;
17251727
}
17261728
ZVAL_STR_COPY(&iterator->current, object->file_name);
17271729
}
17281730
return &iterator->current;
17291731
} else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
17301732
if (Z_ISUNDEF(iterator->current)) {
1731-
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1733+
if (spl_filesystem_object_get_file_name(object) == FAILURE) {
17321734
return NULL;
17331735
}
17341736
spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
@@ -1748,7 +1750,7 @@ static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval
17481750
if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
17491751
ZVAL_STRING(key, object->u.dir.entry.d_name);
17501752
} else {
1751-
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1753+
if (spl_filesystem_object_get_file_name(object) == FAILURE) {
17521754
return;
17531755
}
17541756
ZVAL_STR_COPY(key, object->file_name);
@@ -1860,7 +1862,7 @@ static int spl_filesystem_object_cast(zend_object *readobj, zval *writeobj, int
18601862
}
18611863
/* }}} */
18621864

1863-
static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /* {{{ */
1865+
static zend_result spl_filesystem_file_read(spl_filesystem_object *intern, bool silent) /* {{{ */
18641866
{
18651867
char *buf;
18661868
size_t line_len = 0;
@@ -1909,10 +1911,10 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent) /
19091911
return SUCCESS;
19101912
} /* }}} */
19111913

1912-
static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */
1914+
static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */
19131915
{
19141916
do {
1915-
int ret = spl_filesystem_file_read(intern, 1);
1917+
zend_result ret = spl_filesystem_file_read(intern, 1);
19161918
if (ret != SUCCESS) {
19171919
return ret;
19181920
}
@@ -1934,7 +1936,7 @@ static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char deli
19341936
}
19351937
/* }}} */
19361938

1937-
static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
1939+
static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
19381940
{
19391941
zval retval;
19401942

@@ -1977,7 +1979,7 @@ static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_obje
19771979
}
19781980
} /* }}} */
19791981

1980-
static int spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /* {{{ */
1982+
static bool spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /* {{{ */
19811983
{
19821984
if (intern->u.file.current_line) {
19831985
return intern->u.file.current_line_len == 0;
@@ -2016,9 +2018,9 @@ static int spl_filesystem_file_is_empty_line(spl_filesystem_object *intern) /* {
20162018
}
20172019
/* }}} */
20182020

2019-
static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
2021+
static zend_result spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, int silent) /* {{{ */
20202022
{
2021-
int ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
2023+
zend_result ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
20222024

20232025
while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && spl_filesystem_file_is_empty_line(intern)) {
20242026
spl_filesystem_file_free_line(intern);
@@ -2160,12 +2162,11 @@ PHP_METHOD(SplFileObject, valid)
21602162

21612163
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
21622164
RETURN_BOOL(intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval));
2163-
} else {
2164-
if(!intern->u.file.stream) {
2165-
RETURN_FALSE;
2166-
}
2167-
RETVAL_BOOL(!php_stream_eof(intern->u.file.stream));
21682165
}
2166+
if (!intern->u.file.stream) {
2167+
RETURN_FALSE;
2168+
}
2169+
RETURN_BOOL(!php_stream_eof(intern->u.file.stream));
21692170
} /* }}} */
21702171

21712172
/* {{{ Return next line from file */
@@ -2568,16 +2569,15 @@ PHP_METHOD(SplFileObject, fgetc)
25682569
result = php_stream_getc(intern->u.file.stream);
25692570

25702571
if (result == EOF) {
2571-
RETVAL_FALSE;
2572-
} else {
2573-
if (result == '\n') {
2574-
intern->u.file.current_line_num++;
2575-
}
2576-
buf[0] = result;
2577-
buf[1] = '\0';
2578-
2579-
RETURN_STRINGL(buf, 1);
2572+
RETURN_FALSE;
2573+
}
2574+
if (result == '\n') {
2575+
intern->u.file.current_line_num++;
25802576
}
2577+
buf[0] = result;
2578+
buf[1] = '\0';
2579+
2580+
RETURN_STRINGL(buf, 1);
25812581
} /* }}} */
25822582

25832583
/* {{{ Output all remaining data from a file pointer */
@@ -2734,11 +2734,9 @@ PHP_METHOD(SplFileObject, seek)
27342734
return;
27352735
}
27362736
}
2737-
if (line_pos > 0) {
2738-
if (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2737+
if (line_pos > 0 && !SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
27392738
intern->u.file.current_line_num++;
2740-
spl_filesystem_file_free_line(intern);
2741-
}
2739+
spl_filesystem_file_free_line(intern);
27422740
}
27432741
} /* }}} */
27442742

0 commit comments

Comments
 (0)