Skip to content

Commit 9bbbc9e

Browse files
committed
Add support for union types in stubs
This is the MVP for supporting union types in PHP stubs. Return types with only builtin types work, which is the part we mainly need. Closes GH-4895.
1 parent 70b4bc9 commit 9bbbc9e

File tree

4 files changed

+180
-74
lines changed

4 files changed

+180
-74
lines changed

Zend/zend_API.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ typedef struct _zend_fcall_info_cache {
124124
#define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO(name, class_name, allow_null) \
125125
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, 0, -1, class_name, allow_null)
126126

127+
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
128+
static const zend_internal_arg_info name[] = { \
129+
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_MASK(type | _ZEND_ARG_INFO_FLAGS(return_reference, 0)) },
130+
127131
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
128132
static const zend_internal_arg_info name[] = { \
129133
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_CODE(type, allow_null, _ZEND_ARG_INFO_FLAGS(return_reference, 0)) },

ext/standard/basic_functions.stub.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ function ob_end_flush(): bool {}
2222

2323
function ob_end_clean(): bool {}
2424

25-
/** @return string|false */
26-
function ob_get_flush() {}
25+
function ob_get_flush(): string|false {}
2726

28-
/** @return string|false */
29-
function ob_get_clean() {}
27+
function ob_get_clean(): string|false {}
3028

31-
/** @return string|false */
32-
function ob_get_contents() {}
29+
function ob_get_contents(): string|false {}
3330

3431
function ob_get_level(): int {}
3532

36-
/** @return int|false */
37-
function ob_get_length() {}
33+
function ob_get_length(): int|false {}
3834

3935
function ob_list_handlers(): array {}
4036

@@ -649,26 +645,19 @@ function dirname(string $path, int $levels = 1): string {}
649645
/** @return array|string */
650646
function pathinfo(string $path, int $options = UNKNOWN) {}
651647

652-
/** @return string|false */
653-
function stristr(string $haystack, string $needle, bool $before_needle = false) {}
648+
function stristr(string $haystack, string $needle, bool $before_needle = false): string|false {}
654649

655-
/** @return string|false */
656-
function strstr(string $haystack, string $needle, bool $before_needle = false) {}
650+
function strstr(string $haystack, string $needle, bool $before_needle = false): string|false {}
657651

658-
/** @return int|false */
659-
function strpos(string $haystack, string $needle, int $offset = 0) {}
652+
function strpos(string $haystack, string $needle, int $offset = 0): int|false {}
660653

661-
/** @return int|false */
662-
function stripos(string $haystack, string $needle, int $offset = 0) {}
654+
function stripos(string $haystack, string $needle, int $offset = 0): int|false {}
663655

664-
/** @return int|false */
665-
function strrpos(string $haystack, string $needle, int $offset = 0) {}
656+
function strrpos(string $haystack, string $needle, int $offset = 0): int|false {}
666657

667-
/** @return int|false */
668-
function strripos(string $haystack, string $needle, int $offset = 0) {}
658+
function strripos(string $haystack, string $needle, int $offset = 0): int|false {}
669659

670-
/** @return string|false */
671-
function strrchr(string $haystack, string $needle) {}
660+
function strrchr(string $haystack, string $needle): string|false {}
672661

673662
function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"): string {}
674663

ext/standard/basic_functions_arginfo.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ZEND_END_ARG_INFO()
2323

2424
#define arginfo_ob_end_clean arginfo_ob_flush
2525

26-
ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_get_flush, 0, 0, 0)
26+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ob_get_flush, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
2727
ZEND_END_ARG_INFO()
2828

2929
#define arginfo_ob_get_clean arginfo_ob_get_flush
@@ -33,7 +33,8 @@ ZEND_END_ARG_INFO()
3333
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ob_get_level, 0, 0, IS_LONG, 0)
3434
ZEND_END_ARG_INFO()
3535

36-
#define arginfo_ob_get_length arginfo_ob_get_flush
36+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ob_get_length, 0, 0, MAY_BE_LONG|MAY_BE_FALSE)
37+
ZEND_END_ARG_INFO()
3738

3839
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ob_list_handlers, 0, 0, IS_ARRAY, 0)
3940
ZEND_END_ARG_INFO()
@@ -495,7 +496,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_set_include_path, 0, 0, 1)
495496
ZEND_ARG_TYPE_INFO(0, include_path, IS_STRING, 0)
496497
ZEND_END_ARG_INFO()
497498

498-
#define arginfo_get_include_path arginfo_ob_get_flush
499+
ZEND_BEGIN_ARG_INFO_EX(arginfo_get_include_path, 0, 0, 0)
500+
ZEND_END_ARG_INFO()
499501

500502
#define arginfo_restore_include_path arginfo_flush
501503

@@ -648,7 +650,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dns_get_mx, 0, 2, _IS_BOOL, 0)
648650
ZEND_END_ARG_INFO()
649651
#endif
650652

651-
#define arginfo_net_get_interfaces arginfo_ob_get_flush
653+
#define arginfo_net_get_interfaces arginfo_get_include_path
652654

653655
#if HAVE_FTOK
654656
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftok, 0, 2, IS_LONG, 0)
@@ -674,13 +676,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_md5_file, 0, 0, 1)
674676
ZEND_ARG_TYPE_INFO(0, raw_output, _IS_BOOL, 0)
675677
ZEND_END_ARG_INFO()
676678

677-
#define arginfo_getmyuid arginfo_ob_get_flush
679+
#define arginfo_getmyuid arginfo_get_include_path
678680

679-
#define arginfo_getmygid arginfo_ob_get_flush
681+
#define arginfo_getmygid arginfo_get_include_path
680682

681-
#define arginfo_getmypid arginfo_ob_get_flush
683+
#define arginfo_getmypid arginfo_get_include_path
682684

683-
#define arginfo_getmyinode arginfo_ob_get_flush
685+
#define arginfo_getmyinode arginfo_get_include_path
684686

685687
#define arginfo_getlastmod arginfo_ob_get_level
686688

@@ -873,15 +875,15 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pathinfo, 0, 0, 1)
873875
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
874876
ZEND_END_ARG_INFO()
875877

876-
ZEND_BEGIN_ARG_INFO_EX(arginfo_stristr, 0, 0, 2)
878+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_stristr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
877879
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
878880
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
879881
ZEND_ARG_TYPE_INFO(0, before_needle, _IS_BOOL, 0)
880882
ZEND_END_ARG_INFO()
881883

882884
#define arginfo_strstr arginfo_stristr
883885

884-
ZEND_BEGIN_ARG_INFO_EX(arginfo_strpos, 0, 0, 2)
886+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strpos, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
885887
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
886888
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
887889
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
@@ -893,7 +895,7 @@ ZEND_END_ARG_INFO()
893895

894896
#define arginfo_strripos arginfo_strpos
895897

896-
ZEND_BEGIN_ARG_INFO_EX(arginfo_strrchr, 0, 0, 2)
898+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strrchr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
897899
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
898900
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
899901
ZEND_END_ARG_INFO()
@@ -1112,7 +1114,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chroot, 0, 1, _IS_BOOL, 0)
11121114
ZEND_END_ARG_INFO()
11131115
#endif
11141116

1115-
#define arginfo_getcwd arginfo_ob_get_flush
1117+
#define arginfo_getcwd arginfo_get_include_path
11161118

11171119
#define arginfo_rewinddir arginfo_closedir
11181120

@@ -1239,15 +1241,15 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phpcredits, 0, 0, _IS_BOOL, 0)
12391241
ZEND_ARG_TYPE_INFO(0, flag, IS_LONG, 0)
12401242
ZEND_END_ARG_INFO()
12411243

1242-
#define arginfo_php_sapi_name arginfo_ob_get_flush
1244+
#define arginfo_php_sapi_name arginfo_get_include_path
12431245

12441246
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_php_uname, 0, 0, IS_STRING, 0)
12451247
ZEND_ARG_TYPE_INFO(0, mode, IS_STRING, 0)
12461248
ZEND_END_ARG_INFO()
12471249

1248-
#define arginfo_php_ini_scanned_files arginfo_ob_get_flush
1250+
#define arginfo_php_ini_scanned_files arginfo_get_include_path
12491251

1250-
#define arginfo_php_ini_loaded_file arginfo_ob_get_flush
1252+
#define arginfo_php_ini_loaded_file arginfo_get_include_path
12511253

12521254
ZEND_BEGIN_ARG_INFO_EX(arginfo_iptcembed, 0, 0, 2)
12531255
ZEND_ARG_TYPE_INFO(0, iptcdata, IS_STRING, 0)

0 commit comments

Comments
 (0)