@@ -306,6 +306,7 @@ static void _const_string(smart_str *str, const char *name, zval *value, const c
306
306
static void _function_string (smart_str * str , zend_function * fptr , zend_class_entry * scope , const char * indent );
307
307
static void _property_string (smart_str * str , zend_property_info * prop , const char * prop_name , const char * indent );
308
308
static void _class_const_string (smart_str * str , const zend_string * name , zend_class_constant * c , const char * indent );
309
+ static void _enum_case_string (smart_str * str , const zend_string * name , zend_class_constant * c , const char * indent );
309
310
static void _class_string (smart_str * str , zend_class_entry * ce , zval * obj , const char * indent );
310
311
static void _extension_string (smart_str * str , const zend_module_entry * module , const char * indent );
311
312
static void _zend_extension_string (smart_str * str , const zend_extension * extension , const char * indent );
@@ -330,6 +331,8 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const
330
331
kind = "Interface" ;
331
332
} else if (ce -> ce_flags & ZEND_ACC_TRAIT ) {
332
333
kind = "Trait" ;
334
+ } else if (ce -> ce_flags & ZEND_ACC_ENUM ) {
335
+ kind = "Enum" ;
333
336
}
334
337
smart_str_append_printf (str , "%s%s [ " , indent , kind );
335
338
}
@@ -345,6 +348,8 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const
345
348
smart_str_append_printf (str , "interface " );
346
349
} else if (ce -> ce_flags & ZEND_ACC_TRAIT ) {
347
350
smart_str_append_printf (str , "trait " );
351
+ } else if (ce -> ce_flags & ZEND_ACC_ENUM ) {
352
+ smart_str_append_printf (str , "enum " );
348
353
} else {
349
354
if (ce -> ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS |ZEND_ACC_EXPLICIT_ABSTRACT_CLASS )) {
350
355
smart_str_append_printf (str , "abstract " );
@@ -362,6 +367,12 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const
362
367
smart_str_append_printf (str , " extends %s" , ZSTR_VAL (ce -> parent -> name ));
363
368
}
364
369
370
+ // Show backing type of enums
371
+ if ((ce -> ce_flags & ZEND_ACC_ENUM ) && (ce -> enum_backing_type != IS_UNDEF )) {
372
+ smart_str_append_printf (str ,
373
+ ce -> enum_backing_type == IS_STRING ? ": string" : ": int"
374
+ );
375
+ }
365
376
if (ce -> num_interfaces ) {
366
377
uint32_t i ;
367
378
@@ -384,23 +395,49 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, const
384
395
}
385
396
386
397
/* Constants */
387
- smart_str_append_printf (str , "\n" );
388
- count = zend_hash_num_elements (& ce -> constants_table );
389
- smart_str_append_printf (str , "%s - Constants [%d] {\n" , indent , count );
390
- if (count > 0 ) {
398
+ int total_count = zend_hash_num_elements (& ce -> constants_table );
399
+ int constant_count = 0 ;
400
+ int enum_case_count = 0 ;
401
+ smart_str constant_str = {0 };
402
+ smart_str enum_case_str = {0 };
403
+ /* So that we don't need to loop through all of the constants multiple
404
+ * times (count the constants vs. enum cases, print the constants, print
405
+ * the enum cases) use some temporary helper smart strings. */
406
+ if (total_count > 0 ) {
391
407
zend_string * key ;
392
408
zend_class_constant * c ;
393
409
394
410
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR (CE_CONSTANTS_TABLE (ce ), key , c ) {
395
- _class_const_string (str , key , c , ZSTR_VAL (sub_indent ));
411
+ if (ZEND_CLASS_CONST_FLAGS (c ) & ZEND_CLASS_CONST_IS_CASE ) {
412
+ _enum_case_string (& enum_case_str , key , c , ZSTR_VAL (sub_indent ));
413
+ enum_case_count ++ ;
414
+ } else {
415
+ _class_const_string (& constant_str , key , c , ZSTR_VAL (sub_indent ));
416
+ constant_count ++ ;
417
+ }
396
418
if (UNEXPECTED (EG (exception ))) {
397
419
zend_string_release (sub_indent );
420
+ smart_str_free (& enum_case_str );
421
+ smart_str_free (& constant_str );
398
422
return ;
399
423
}
400
424
} ZEND_HASH_FOREACH_END ();
401
425
}
426
+ // Enum cases go first, but the heading is only shown if there are any
427
+ if (enum_case_count ) {
428
+ smart_str_append_printf (str , "\n" );
429
+ smart_str_append_printf (str , "%s - Enum cases [%d] {\n" , indent , enum_case_count );
430
+ smart_str_append_smart_str (str , & enum_case_str );
431
+ smart_str_append_printf (str , "%s }\n" , indent );
432
+ }
433
+ smart_str_append_printf (str , "\n" );
434
+ smart_str_append_printf (str , "%s - Constants [%d] {\n" , indent , constant_count );
435
+ smart_str_append_smart_str (str , & constant_str );
402
436
smart_str_append_printf (str , "%s }\n" , indent );
403
437
438
+ smart_str_free (& enum_case_str );
439
+ smart_str_free (& constant_str );
440
+
404
441
/* Static properties */
405
442
/* counting static properties */
406
443
count = zend_hash_num_elements (& ce -> properties_info );
@@ -626,6 +663,32 @@ static void _class_const_string(smart_str *str, const zend_string *name, zend_cl
626
663
}
627
664
/* }}} */
628
665
666
+ static void _enum_case_string (smart_str * str , const zend_string * name , zend_class_constant * c , const char * indent )
667
+ {
668
+ if (Z_TYPE (c -> value ) == IS_CONSTANT_AST && zend_update_class_constant (c , name , c -> ce ) == FAILURE ) {
669
+ return ;
670
+ }
671
+
672
+ if (c -> doc_comment ) {
673
+ smart_str_append_printf (str , "%s%s\n" , indent , ZSTR_VAL (c -> doc_comment ));
674
+ }
675
+ smart_str_append_printf (str , "%sCase %s" , indent , ZSTR_VAL (name ));
676
+ if (c -> ce -> enum_backing_type == IS_UNDEF ) {
677
+ // No value
678
+ smart_str_appends (str , "\n" );
679
+ } else {
680
+ /* Has a value, which is the enum instance, get the value from that.
681
+ * We know it must be either a string or integer so no need
682
+ * for the IS_ARRAY or IS_OBJECT handling that _class_const_string()
683
+ * requires. */
684
+ zval * enum_val = zend_enum_fetch_case_value (Z_OBJ (c -> value ));
685
+ zend_string * tmp_value_str ;
686
+ zend_string * value_str = zval_get_tmp_string (enum_val , & tmp_value_str );
687
+ smart_str_append_printf (str , " = %s\n" , ZSTR_VAL (value_str ));
688
+ zend_tmp_string_release (tmp_value_str );
689
+ }
690
+ }
691
+
629
692
static zend_op * get_recv_op (const zend_op_array * op_array , uint32_t offset )
630
693
{
631
694
zend_op * op = op_array -> opcodes ;
0 commit comments