Skip to content

PHPC-1070 and PHPC-1071: Report class name for unexpected object values #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2017

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Dec 14, 2017

https://jira.mongodb.org/browse/PHPC-1070
https://jira.mongodb.org/browse/PHPC-1071

Adds a macro for accessing zval's class or type name, depending on whether it's an object type. This replaces existing ternary statements we had for some BSON exceptions. I've also gone through and updated exceptions where object types are entirely unexpected. Options that expect a BSON document (i.e. array or object) remain unchanged and do not use this macro, as any object type would be accepted.

IMO, this makes the exceptions more helpful and also makes PHPC more consistent with the invalid type exception messages in PHPLIB.

);
zval_ptr_dtor(&obj_data);
#else
if (Z_TYPE_P(obj_data) != IS_ARRAY && !(Z_TYPE_P(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(obj_data), zend_standard_class_def TSRMLS_CC))) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
"Expected %s::%s() to return an array or stdClass, %s given",
Z_OBJCE_P(object)->name,
ZSTR_VAL(Z_OBJCE_P(object)->name),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this isn't necessary since we're in PHP 5 mode here, but it reduces some variation between the PHP 5 and 7 blocks. The only real difference in these blocks is that obj_data is a zval * for PHP 5.

#else
Z_OBJCE_P(data)->name,
#endif
ZSTR_VAL(Z_OBJCE_P(data)->name),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have eliminated the #if block much earlier, but I just noticed this.

(Z_TYPE_P(obj_data) == IS_OBJECT
? Z_OBJCE_P(obj_data)->name
: zend_get_type_by_const(Z_TYPE_P(obj_data))
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only difference here is that obj_data is a zval * for PHP 5.

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.
@jmikola jmikola merged commit 03af904 into mongodb:master Dec 15, 2017
jmikola added a commit that referenced this pull request Dec 15, 2017
@jmikola jmikola deleted the phpc-1070 branch December 15, 2017 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants