Skip to content

Commit 0d767a7

Browse files
committed
PHPC-1073: Refactor functions to early exit on missing elements
1 parent 910b5e7 commit 0d767a7

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

src/MongoDB/Command.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,26 @@ zend_class_entry* php_phongo_command_ce;
3535
* via mongoc_cursor_set_max_await_time_ms(). */
3636
static bool php_phongo_command_init_max_await_time_ms(php_phongo_command_t* intern, zval* options TSRMLS_DC) /* {{{ */
3737
{
38-
if (php_array_existsc(options, "maxAwaitTimeMS")) {
39-
int64_t max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
38+
int64_t max_await_time_ms;
4039

41-
if (max_await_time_ms < 0) {
42-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
43-
return false;
44-
}
40+
if (!php_array_existsc(options, "maxAwaitTimeMS")) {
41+
return true;
42+
}
4543

46-
if (max_await_time_ms > UINT32_MAX) {
47-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
48-
return false;
49-
}
44+
max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
5045

51-
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
46+
if (max_await_time_ms < 0) {
47+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
48+
return false;
5249
}
5350

51+
if (max_await_time_ms > UINT32_MAX) {
52+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
53+
return false;
54+
}
55+
56+
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
57+
5458
return true;
5559
} /* }}} */
5660

src/MongoDB/Query.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,21 @@ static bool php_phongo_query_init_limit_and_singlebatch(php_phongo_query_t* inte
203203
* which must be converted to a mongoc_read_concern_t. */
204204
static bool php_phongo_query_init_readconcern(php_phongo_query_t* intern, zval* options TSRMLS_DC) /* {{{ */
205205
{
206-
if (php_array_existsc(options, "readConcern")) {
207-
zval* read_concern = php_array_fetchc(options, "readConcern");
206+
zval* read_concern;
208207

209-
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
210-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
211-
return false;
212-
}
208+
if (!php_array_existsc(options, "readConcern")) {
209+
return true;
210+
}
211+
212+
read_concern = php_array_fetchc(options, "readConcern");
213213

214-
intern->read_concern = mongoc_read_concern_copy(phongo_read_concern_from_zval(read_concern TSRMLS_CC));
214+
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
215+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(read_concern));
216+
return false;
215217
}
216218

219+
intern->read_concern = mongoc_read_concern_copy(phongo_read_concern_from_zval(read_concern TSRMLS_CC));
220+
217221
return true;
218222
} /* }}} */
219223

@@ -224,22 +228,26 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t* intern, zval*
224228
* via mongoc_cursor_set_max_await_time_ms(). */
225229
static bool php_phongo_query_init_max_await_time_ms(php_phongo_query_t* intern, zval* options TSRMLS_DC) /* {{{ */
226230
{
227-
if (php_array_existsc(options, "maxAwaitTimeMS")) {
228-
int64_t max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
231+
int64_t max_await_time_ms;
229232

230-
if (max_await_time_ms < 0) {
231-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
232-
return false;
233-
}
233+
if (!php_array_existsc(options, "maxAwaitTimeMS")) {
234+
return true;
235+
}
234236

235-
if (max_await_time_ms > UINT32_MAX) {
236-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
237-
return false;
238-
}
237+
max_await_time_ms = php_array_fetchc_long(options, "maxAwaitTimeMS");
239238

240-
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
239+
if (max_await_time_ms < 0) {
240+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be >= 0, %" PRId64 " given", max_await_time_ms);
241+
return false;
241242
}
242243

244+
if (max_await_time_ms > UINT32_MAX) {
245+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"maxAwaitTimeMS\" option to be <= %" PRIu32 ", %" PRId64 " given", UINT32_MAX, max_await_time_ms);
246+
return false;
247+
}
248+
249+
intern->max_await_time_ms = (uint32_t) max_await_time_ms;
250+
243251
return true;
244252
} /* }}} */
245253

0 commit comments

Comments
 (0)