Skip to content

Commit 0c84160

Browse files
committed
streams: refactor _php_stream_opendir() to use early returns
1 parent 5226e2f commit 0c84160

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

main/streams/streams.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,21 +2034,32 @@ PHPAPI php_stream *_php_stream_opendir(const char *path, int options,
20342034
path_to_open = path;
20352035

20362036
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options);
2037-
2038-
if (wrapper && wrapper->wops->dir_opener) {
2039-
stream = wrapper->wops->dir_opener(wrapper,
2040-
path_to_open, "r", options & ~REPORT_ERRORS, NULL,
2041-
context STREAMS_REL_CC);
2042-
2043-
if (stream) {
2044-
stream->wrapper = wrapper;
2045-
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR;
2037+
if (UNEXPECTED(wrapper == NULL)) {
2038+
if (options & REPORT_ERRORS) {
2039+
php_stream_display_wrapper_errors(NULL, context, PHP_STREAM_EC(OpenFailed),
2040+
"Failed to open directory");
2041+
php_stream_tidy_wrapper_error_log(wrapper);
20462042
}
2047-
} else if (wrapper) {
2043+
return NULL;
2044+
}
2045+
2046+
if (UNEXPECTED(!wrapper->wops->dir_opener)) {
20482047
php_stream_wrapper_log_warn(wrapper, context, options & ~REPORT_ERRORS,
20492048
NoOpener, "not implemented");
2049+
php_stream_display_wrapper_errors(wrapper, context, PHP_STREAM_EC(OpenFailed),
2050+
"Failed to open directory");
2051+
php_stream_tidy_wrapper_error_log(wrapper);
2052+
return NULL;
20502053
}
2051-
if (stream == NULL && (options & REPORT_ERRORS)) {
2054+
2055+
stream = wrapper->wops->dir_opener(wrapper,
2056+
path_to_open, "r", options & ~REPORT_ERRORS, NULL,
2057+
context STREAMS_REL_CC);
2058+
2059+
if (stream) {
2060+
stream->wrapper = wrapper;
2061+
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR;
2062+
} else if (options & REPORT_ERRORS) {
20522063
php_stream_display_wrapper_errors(wrapper, context, PHP_STREAM_EC(OpenFailed),
20532064
"Failed to open directory");
20542065
}

0 commit comments

Comments
 (0)