Skip to content

Commit f4e68a3

Browse files
committed
Revert "Do not prepend ? on nullables in ReflectionType::__toString()"
This reverts commit 8855a2c.
1 parent 129e8da commit f4e68a3

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

NEWS

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ PHP NEWS
1212
images). (cmb)
1313
. Fixed bug #68716 (possible resource leaks in _php_image_convert()). (cmb)
1414

15-
- Reflection:
16-
. Reverted prepending \ for class names and ? for nullable types returned
17-
from ReflectionType::__toString(). (Trowski)
18-
1915
- Intl:
2016
. Fixed bug #65732 (grapheme_*() is not Unicode compliant on CR LF
2117
sequence). (cmb)
2218

2319
- Reflection:
24-
. Reverted prepending \ for class names and ? for nullable types returned
25-
from ReflectionType::__toString(). (Trowski)
20+
. Reverted prepending \ for class names. (Trowski)
2621

2722
- Standard:
2823
. Fixed bug #72920 (Accessing a private constant using constant() creates
@@ -88,6 +83,8 @@ PHP NEWS
8883
should match). (cmb).
8984
. Add ReflectionNamedType::getName(). This method should be used instead of
9085
ReflectionType::__toString()
86+
. Prepend \ for class names and ? for nullable types returned from
87+
ReflectionType::__toString(). (Trowski)
9188

9289
- Session:
9390
. Implemented RFC: Session ID without hashing. (Yasuo)

UPGRADING

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ PHP 7.1 UPGRADE NOTES
124124
. The behavior of ReflectionMethod::invoke() and ::invokeArgs() has been
125125
aligned, what causes slightly different behavior than before for some
126126
pathological cases.
127+
. ReflectionType::__toString() will now return the type name with a leading
128+
"?" if it is nullable. To retrieve the type name without leading "?" the new
129+
ReflectionNamedType::getName() method can be used.
127130

128131
========================================
129132
2. New Features
@@ -305,9 +308,6 @@ PHP 7.1 UPGRADE NOTES
305308
- Reflection:
306309
. Failure to retrieve a reflection object or retrieve an object property
307310
will now throw an instance of Error instead of resulting in a fatal error.
308-
. ReflectionNamedType will be returned from ReflectionParameter::getType()
309-
and ReflectionFunctionAbstract::getReturnType(). This class includes a
310-
getName() method that returns the type name as a string.
311311

312312
- Session:
313313
. Custom session handlers that do not return strings for session IDs will

ext/reflection/php_reflection.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3028,13 +3028,23 @@ ZEND_METHOD(reflection_type, __toString)
30283028
{
30293029
reflection_object *intern;
30303030
type_reference *param;
3031+
zend_string *str;
30313032

30323033
if (zend_parse_parameters_none() == FAILURE) {
30333034
return;
30343035
}
30353036
GET_REFLECTION_OBJECT_PTR(param);
30363037

3037-
RETURN_STR(reflection_type_name(param));
3038+
str = reflection_type_name(param);
3039+
3040+
if (param->arg_info->allow_null) {
3041+
size_t orig_len = ZSTR_LEN(str);
3042+
str = zend_string_extend(str, orig_len + 1, 0);
3043+
memmove(ZSTR_VAL(str) + 1, ZSTR_VAL(str), orig_len + 1);
3044+
ZSTR_VAL(str)[0] = '?';
3045+
}
3046+
3047+
RETURN_STR(str);
30383048
}
30393049
/* }}} */
30403050

ext/reflection/tests/ReflectionNamedType.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ var_dump((string) $return);
3232
?>
3333
--EXPECTF--
3434
string(11) "Traversable"
35-
string(11) "Traversable"
36-
string(6) "string"
35+
string(12) "?Traversable"
3736
string(6) "string"
37+
string(7) "?string"
3838
string(4) "Test"
39+
string(5) "?Test"
3940
string(4) "Test"
40-
string(4) "Test"
41-
string(4) "Test"
41+
string(5) "?Test"

ext/reflection/tests/ReflectionType_001.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ foreach ([
7373
var_dump((string)$ra);
7474
}
7575
}
76+
77+
?>
7678
--EXPECT--
7779
*** functions
7880
** Function 0 - Parameter 0
@@ -94,7 +96,7 @@ string(8) "callable"
9496
bool(true)
9597
bool(true)
9698
bool(false)
97-
string(8) "stdClass"
99+
string(9) "?stdClass"
98100
** Function 0 - Parameter 4
99101
bool(false)
100102
** Function 0 - Parameter 5

0 commit comments

Comments
 (0)