Skip to content

Commit 03af904

Browse files
committed
PHPC-1071: Report class name for unexpected object values
This only updates exceptions where object types are entirely unexpected. For instance, options that expect a BSON document (i.e. array or object) need not use this macro, as any object would be accepted.
1 parent f800906 commit 03af904

13 files changed

+20
-20
lines changed

php_phongo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static bool process_read_concern(zval *option, bson_t *mongoc_opts TSRMLS_DC)
469469
phongo_throw_exception(
470470
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
471471
"Expected 'readConcern' option to be 'MongoDB\\Driver\\ReadConcern', %s given",
472-
zend_get_type_by_const(Z_TYPE_P(option))
472+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
473473
);
474474
return false;
475475
}
@@ -520,7 +520,7 @@ static bool process_read_preference(zval *option, bson_t *mongoc_opts, zval **zr
520520
phongo_throw_exception(
521521
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
522522
"Expected 'readPreference' option to be 'MongoDB\\Driver\\ReadPreference', %s given",
523-
zend_get_type_by_const(Z_TYPE_P(option))
523+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
524524
);
525525
return false;
526526
}
@@ -543,7 +543,7 @@ static bool process_write_concern(zval *option, bson_t *mongoc_opts, zval **zwri
543543
phongo_throw_exception(
544544
PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC,
545545
"Expected 'writeConcern' option to be 'MongoDB\\Driver\\WriteConcern', %s given",
546-
zend_get_type_by_const(Z_TYPE_P(option))
546+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(option)
547547
);
548548
return false;
549549
}

src/BSON/Timestamp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static PHP_METHOD(Timestamp, __construct)
147147
}
148148

149149
if (Z_TYPE_P(increment) != IS_STRING) {
150-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(increment)));
150+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(increment));
151151
return;
152152
}
153153

@@ -156,7 +156,7 @@ static PHP_METHOD(Timestamp, __construct)
156156
}
157157

158158
if (Z_TYPE_P(timestamp) != IS_STRING) {
159-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(timestamp)));
159+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(timestamp));
160160
return;
161161
}
162162

src/BSON/UTCDateTime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static PHP_METHOD(UTCDateTime, __construct)
190190
}
191191

192192
if (Z_TYPE_P(milliseconds) != IS_STRING) {
193-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(milliseconds)));
193+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
194194
return;
195195
}
196196

src/MongoDB/Query.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static bool php_phongo_query_opts_append_string(bson_t *opts, const char *opts_k
3535
zval *value = php_array_fetch(zarr, zarr_key);
3636

3737
if (Z_TYPE_P(value) != IS_STRING) {
38-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
38+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be string, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
3939
return false;
4040
}
4141

@@ -55,7 +55,7 @@ static bool php_phongo_query_opts_append_document(bson_t *opts, const char *opts
5555
bson_t b = BSON_INITIALIZER;
5656

5757
if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_ARRAY) {
58-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", zend_get_type_by_const(Z_TYPE_P(value)));
58+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"%s\" %s to be array or object, %s given", zarr_key, zarr_key[0] == '$' ? "modifier" : "option", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(value));
5959
return false;
6060
}
6161

@@ -195,7 +195,7 @@ static bool php_phongo_query_init_readconcern(php_phongo_query_t *intern, zval *
195195
zval *read_concern = php_array_fetchc(options, "readConcern");
196196

197197
if (Z_TYPE_P(read_concern) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(read_concern), php_phongo_readconcern_ce TSRMLS_CC)) {
198-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"readConcern\" option to be %s, %s given", ZSTR_VAL(php_phongo_readconcern_ce->name), zend_get_type_by_const(Z_TYPE_P(read_concern)));
198+
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));
199199
return false;
200200
}
201201

@@ -262,7 +262,7 @@ static bool php_phongo_query_init(php_phongo_query_t *intern, zval *filter, zval
262262
modifiers = php_array_fetchc(options, "modifiers");
263263

264264
if (Z_TYPE_P(modifiers) != IS_ARRAY) {
265-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", zend_get_type_by_const(Z_TYPE_P(modifiers)));
265+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected \"modifiers\" option to be array, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(modifiers));
266266
return false;
267267
}
268268
}

src/MongoDB/ReadPreference.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static PHP_METHOD(ReadPreference, __construct)
8080
return;
8181
}
8282
} else {
83-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(mode)));
83+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected mode to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(mode));
8484
return;
8585
}
8686

src/MongoDB/WriteConcern.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static PHP_METHOD(WriteConcern, __construct)
6262
mongoc_write_concern_set_wtag(intern->write_concern, Z_STRVAL_P(w));
6363
}
6464
} else {
65-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(w)));
65+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(w));
6666
return;
6767
}
6868

tests/bson/bson-timestamp_error-006.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Expected increment to be an unsigned 32-bit integer or string, boolean given
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected increment to be an unsigned 32-bit integer or string, array given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected increment to be an unsigned 32-bit integer or string, object given
35+
Expected increment to be an unsigned 32-bit integer or string, stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Expected timestamp to be an unsigned 32-bit integer or string, %r(null|NULL)%r given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
@@ -42,5 +42,5 @@ Expected timestamp to be an unsigned 32-bit integer or string, boolean given
4242
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4343
Expected timestamp to be an unsigned 32-bit integer or string, array given
4444
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
45-
Expected timestamp to be an unsigned 32-bit integer or string, object given
45+
Expected timestamp to be an unsigned 32-bit integer or string, stdClass given
4646
===DONE===

tests/manager/manager-executeCommand_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo throws(function() use ($manager, $command) {
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
35+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Unknown option 'unknown'
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/manager/manager-executeQuery_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ echo throws(function() use ($manager, $query) {
3232
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3333
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
35+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3737
Unknown option 'unknown'
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/query/query-ctor_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Expected "readConcern" option to be MongoDB\Driver\ReadConcern, boolean given
3737
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3838
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, array given
3939
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
40-
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, object given
40+
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, stdClass given
4141
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4242
Expected "readConcern" option to be MongoDB\Driver\ReadConcern, %r(null|NULL)%r given
4343
===DONE===

tests/server/server-executeCommand_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo throws(function() use ($manager, $command) {
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3535
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
37+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3939
Unknown option 'unknown'
4040
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/server/server-executeQuery_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo throws(function() use ($manager, $query) {
3434
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3535
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', string given
3636
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37-
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', object given
37+
Expected 'readPreference' option to be 'MongoDB\Driver\ReadPreference', stdClass given
3838
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3939
Unknown option 'unknown'
4040
OK: Got MongoDB\Driver\Exception\InvalidArgumentException

tests/writeConcern/writeconcern-ctor_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Expected w to be integer or string, boolean given
3131
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3232
Expected w to be integer or string, array given
3333
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34-
Expected w to be integer or string, object given
34+
Expected w to be integer or string, stdClass given
3535
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3636
Expected w to be integer or string, %r(null|NULL)%r given
3737
===DONE===

0 commit comments

Comments
 (0)