Skip to content

Commit fdc71b4

Browse files
committed
Merge pull request #43 from jmikola/phpc-283
PHPC-283: UnexpectedValueException and bsonSerialize() cleanup
2 parents b8b41a5 + 7add8ae commit fdc71b4

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/bson.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
619619
}
620620

621621
if (Z_TYPE_P(obj_data) != IS_ARRAY) {
622-
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
622+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
623623
zval_ptr_dtor(&obj_data);
624624

625625
return;
@@ -767,7 +767,7 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
767767
PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *bson, bson_t **bson_out TSRMLS_DC) /* {{{ */
768768
{
769769
HashPosition pos;
770-
HashTable *ht_data;
770+
HashTable *ht_data = NULL;
771771
zval *obj_data = NULL;
772772

773773
switch(Z_TYPE_P(data)) {
@@ -777,14 +777,13 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
777777

778778
if (!obj_data) {
779779
/* zend_call_method() failed */
780-
return;
780+
break;
781781
}
782782

783783
if (Z_TYPE_P(obj_data) != IS_ARRAY) {
784-
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
785-
zval_ptr_dtor(&obj_data);
784+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data)));
786785

787-
return;
786+
break;
788787
}
789788

790789
ht_data = HASH_OF(obj_data);
@@ -807,7 +806,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
807806
return;
808807
}
809808

810-
if (ht_data && ht_data->nApplyCount > 1) {
809+
if (!ht_data || ht_data->nApplyCount > 1) {
811810
if (obj_data) {
812811
zval_ptr_dtor(&obj_data);
813812
}

tests/bson/bson-encode_error-001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $invalidValues = array(new stdClass, 'foo', 1, true);
2626
foreach ($invalidValues as $invalidValue) {
2727
try {
2828
BSON\fromArray(new MyClass($invalidValue));
29-
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
29+
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
3030
echo $e->getMessage(), "\n";
3131
}
3232
}

tests/bson/bson-encode_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $invalidValues = array(new stdClass, 'foo', 1, true);
2626
foreach ($invalidValues as $invalidValue) {
2727
try {
2828
$bson = BSON\fromArray(array('embed' => new MyClass($invalidValue)));
29-
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
29+
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
3030
echo $e->getMessage(), "\n";
3131
}
3232
}

0 commit comments

Comments
 (0)