Skip to content

Commit 00a3e01

Browse files
bjoriderickr
authored andcommitted
Simplify IS_ARRAY/IS_OBJECT discovery for PHP7
1 parent d18eb0e commit 00a3e01

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/bson.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -663,34 +663,43 @@ bool php_phongo_bson_visit_array(const bson_iter_t *iter ARG_UNUSED, const char
663663
}
664664

665665

666-
int php_phongo_is_array_or_document(zval **val TSRMLS_DC) /* {{{ */
666+
int php_phongo_is_array_or_document(zval *val TSRMLS_DC) /* {{{ */
667667
{
668-
HashTable *ht_data = HASH_OF(*val);
668+
HashTable *ht_data = HASH_OF(val);
669669
int count;
670670

671-
if (Z_TYPE_PP(val) != IS_ARRAY) {
671+
if (Z_TYPE_P(val) != IS_ARRAY) {
672672
return IS_OBJECT;
673673
}
674674

675675
count = ht_data ? zend_hash_num_elements(ht_data) : 0;
676676
if (count > 0) {
677-
char *key;
678-
unsigned int key_len;
679-
unsigned long index = 0;
680-
unsigned long idx = 0;
681-
int hash_type = 0;
682-
HashPosition pos;
683677
#if PHP_VERSION_ID >= 70000
684-
zend_string *zs_key;
685-
#endif
678+
zend_string *key;
679+
zend_ulong index, idx;
680+
681+
idx = 0;
682+
ZEND_HASH_FOREACH_KEY(ht_data, index, key) {
683+
if (key) {
684+
return IS_OBJECT;
685+
} else {
686+
if (index != idx) {
687+
return IS_OBJECT;
688+
}
689+
}
690+
idx++;
691+
} ZEND_HASH_FOREACH_END();
692+
#else
693+
char *key;
694+
unsigned int key_len;
695+
unsigned long index = 0;
696+
unsigned long idx = 0;
697+
int hash_type = 0;
698+
HashPosition pos;
686699

687700
zend_hash_internal_pointer_reset_ex(ht_data, &pos);
688701
for (;; zend_hash_move_forward_ex(ht_data, &pos)) {
689-
#if PHP_VERSION_ID >= 70000
690-
hash_type = zend_hash_get_current_key_ex(ht_data, &zs_key, &index, &pos);
691-
#else
692702
hash_type = zend_hash_get_current_key_ex(ht_data, &key, &key_len, &index, 0, &pos);
693-
#endif
694703
if (hash_type == HASH_KEY_NON_EXISTENT) {
695704
break;
696705
}
@@ -704,8 +713,9 @@ int php_phongo_is_array_or_document(zval **val TSRMLS_DC) /* {{{ */
704713
}
705714
idx++;
706715
}
716+
#endif
707717
} else {
708-
return Z_TYPE_PP(val);
718+
return Z_TYPE_P(val);
709719
}
710720

711721
return IS_ARRAY;

0 commit comments

Comments
 (0)