Skip to content

Commit cec8ad0

Browse files
committed
Make false|null be represented as ?false
1 parent f9d5360 commit cec8ad0

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
12551255

12561256
if (type_mask & MAY_BE_NULL) {
12571257
bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1258-
if (!is_union && !zend_string_equals_literal(str, "false")) {
1258+
if (!is_union) {
12591259
zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
12601260
zend_string_release(str);
12611261
return nullable_str;

ext/reflection/php_reflection.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,6 @@ static reflection_type_kind get_type_kind(zend_type type) {
13421342
if (type_mask_without_null == MAY_BE_BOOL || ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
13431343
return NAMED_TYPE;
13441344
}
1345-
/* null|false must be a union type */
1346-
if (ZEND_TYPE_PURE_MASK(type) == (MAY_BE_NULL|MAY_BE_FALSE)) {
1347-
return UNION_TYPE;
1348-
}
13491345
/* Check that only one bit is set. */
13501346
if ((type_mask_without_null & (type_mask_without_null - 1)) != 0) {
13511347
return UNION_TYPE;

ext/reflection/tests/union_types.phpt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ function dumpType(ReflectionUnionType $rt) {
1313
}
1414
}
1515

16+
function dumpBCType(ReflectionNamedType $rt) {
17+
echo "Type $rt:\n";
18+
echo " Name: " . $rt->getName() . "\n";
19+
echo " String: " . (string) $rt . "\n";
20+
echo " Allows Null: " . ($rt->allowsNull() ? "true" : "false") . "\n";
21+
}
22+
1623
function test1(): X|Y|int|float|false|null { }
1724
function test2(): X|iterable|bool { }
1825
function test3(): null|false { }
@@ -24,8 +31,8 @@ class Test {
2431

2532
dumpType((new ReflectionFunction('test1'))->getReturnType());
2633
dumpType((new ReflectionFunction('test2'))->getReturnType());
27-
dumpType((new ReflectionFunction('test3'))->getReturnType());
28-
dumpType((new ReflectionFunction('test4'))->getReturnType());
34+
dumpBCType((new ReflectionFunction('test3'))->getReturnType());
35+
dumpBCType((new ReflectionFunction('test4'))->getReturnType());
2936

3037
$rc = new ReflectionClass(Test::class);
3138
$rp = $rc->getProperty('prop');
@@ -79,21 +86,13 @@ Allows null: false
7986
Name: bool
8087
String: bool
8188
Allows Null: false
82-
Type false|null:
83-
Allows null: true
89+
Type ?false:
8490
Name: false
85-
String: false
86-
Allows Null: false
87-
Name: null
88-
String: null
91+
String: ?false
8992
Allows Null: true
90-
Type false|null:
91-
Allows null: true
93+
Type ?false:
9294
Name: false
93-
String: false
94-
Allows Null: false
95-
Name: null
96-
String: null
95+
String: ?false
9796
Allows Null: true
9897
Type X|Y|int:
9998
Allows null: false

0 commit comments

Comments
 (0)