Skip to content

Commit 01eb06a

Browse files
committed
Follow-up fix for GH-9655
Type needs to be rendered as a DNF type and not X&Y|null
1 parent 1e9280e commit 01eb06a

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Zend/tests/type_declarations/intersection_types/implicit_nullable_intersection_type.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ function foo(X&Y $foo = null) {
1010
foo(null);
1111

1212
?>
13-
--EXPECTF--
13+
--EXPECT--
1414
NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Verify type rendering in type error for implicitly nullable intersection types
3+
--FILE--
4+
<?php
5+
6+
function foo(X&Y $foo = null) {
7+
var_dump($foo);
8+
}
9+
10+
try {
11+
foo(5);
12+
} catch (\TypeError $e) {
13+
echo $e->getMessage(), \PHP_EOL;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
foo(): Argument #1 ($foo) must be of type (X&Y)|null, int given, called in %s on line %d

Zend/zend_compile.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,12 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
12301230
/* Pure intersection type */
12311231
if (ZEND_TYPE_IS_INTERSECTION(type)) {
12321232
ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1233-
str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1233+
bool is_bracketed = false;
1234+
/* Shim for implicitly nullable pure intersection types */
1235+
if (UNEXPECTED(ZEND_TYPE_PURE_MASK(type) & MAY_BE_NULL)) {
1236+
is_bracketed = true;
1237+
}
1238+
str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, is_bracketed);
12341239
} else if (ZEND_TYPE_HAS_LIST(type)) {
12351240
/* A union type might not be a list */
12361241
zend_type *list_type;

0 commit comments

Comments
 (0)