Skip to content

Commit f800906

Browse files
committed
PHPC-1070: Macro for accessing zval class or type name
1 parent 983aed6 commit f800906

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

php_phongo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "bson.h"
2222
#include "mongoc.h"
2323

24+
#include "phongo_compat.h"
25+
#include "php_phongo_classes.h"
26+
2427
#define phpext_mongodb_ptr &mongodb_module_entry
2528
extern zend_module_entry mongodb_module_entry;
2629

@@ -61,8 +64,6 @@ ZEND_TSRMLS_CACHE_EXTERN()
6164

6265
#define PHONGO_WRITE_CONCERN_W_MAJORITY "majority"
6366

64-
#include "php_phongo_classes.h"
65-
6667
/* This enum is necessary since mongoc_server_description_type_t is private and
6768
* we need to translate strings returned by mongoc_server_description_type() to
6869
* Server integer constants. */
@@ -194,6 +195,9 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_
194195
} \
195196
} while(0);
196197

198+
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME(zv) (Z_TYPE(zv) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE(zv)->name) : zend_get_type_by_const(Z_TYPE(zv)))
199+
#define PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(zvp) PHONGO_ZVAL_CLASS_OR_TYPE_NAME(*(zvp))
200+
197201
#endif /* PHONGO_H */
198202

199203

src/bson-encode.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,18 @@ static void php_phongo_bson_append_object(bson_t *bson, php_phongo_bson_flags_t
143143
if (Z_TYPE(obj_data) != IS_ARRAY && !(Z_TYPE(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE(obj_data), zend_standard_class_def TSRMLS_CC))) {
144144
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
145145
"Expected %s::%s() to return an array or stdClass, %s given",
146-
Z_OBJCE_P(object)->name->val,
146+
ZSTR_VAL(Z_OBJCE_P(object)->name),
147147
BSON_SERIALIZE_FUNC_NAME,
148-
(Z_TYPE(obj_data) == IS_OBJECT
149-
? Z_OBJCE(obj_data)->name->val
150-
: zend_get_type_by_const(Z_TYPE(obj_data))
151-
)
148+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
152149
);
153150
zval_ptr_dtor(&obj_data);
154151
#else
155152
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))) {
156153
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
157154
"Expected %s::%s() to return an array or stdClass, %s given",
158-
Z_OBJCE_P(object)->name,
155+
ZSTR_VAL(Z_OBJCE_P(object)->name),
159156
BSON_SERIALIZE_FUNC_NAME,
160-
(Z_TYPE_P(obj_data) == IS_OBJECT
161-
? Z_OBJCE_P(obj_data)->name
162-
: zend_get_type_by_const(Z_TYPE_P(obj_data))
163-
)
157+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
164158
);
165159
zval_ptr_dtor(&obj_data);
166160
#endif
@@ -453,22 +447,13 @@ void php_phongo_zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
453447
#endif
454448
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC,
455449
"Expected %s::%s() to return an array or stdClass, %s given",
456-
#if PHP_VERSION_ID >= 70000
457-
Z_OBJCE_P(data)->name->val,
458-
#else
459-
Z_OBJCE_P(data)->name,
460-
#endif
450+
ZSTR_VAL(Z_OBJCE_P(data)->name),
461451
BSON_SERIALIZE_FUNC_NAME,
462452
#if PHP_VERSION_ID >= 70000
463-
(Z_TYPE(obj_data) == IS_OBJECT
464-
? Z_OBJCE(obj_data)->name->val
465-
: zend_get_type_by_const(Z_TYPE(obj_data))
453+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME(obj_data)
466454
#else
467-
(Z_TYPE_P(obj_data) == IS_OBJECT
468-
? Z_OBJCE_P(obj_data)->name
469-
: zend_get_type_by_const(Z_TYPE_P(obj_data))
455+
PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(obj_data)
470456
#endif
471-
)
472457
);
473458

474459
goto cleanup;

0 commit comments

Comments
 (0)