From f4e68a3968429757719c600ce16913ab09539f0d Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Tue, 23 Aug 2016 09:42:04 -0600 Subject: [PATCH] Revert "Do not prepend ? on nullables in ReflectionType::__toString()" This reverts commit 8855a2ce76e8bfba1d2eea1345c765fde7a9a441. --- NEWS | 9 +++------ UPGRADING | 6 +++--- ext/reflection/php_reflection.c | 12 +++++++++++- ext/reflection/tests/ReflectionNamedType.phpt | 8 ++++---- ext/reflection/tests/ReflectionType_001.phpt | 4 +++- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index ca0f198f945b4..8b5f0f6aa4338 100644 --- a/NEWS +++ b/NEWS @@ -12,17 +12,12 @@ PHP NEWS images). (cmb) . Fixed bug #68716 (possible resource leaks in _php_image_convert()). (cmb) -- Reflection: - . Reverted prepending \ for class names and ? for nullable types returned - from ReflectionType::__toString(). (Trowski) - - Intl: . Fixed bug #65732 (grapheme_*() is not Unicode compliant on CR LF sequence). (cmb) - Reflection: - . Reverted prepending \ for class names and ? for nullable types returned - from ReflectionType::__toString(). (Trowski) + . Reverted prepending \ for class names. (Trowski) - Standard: . Fixed bug #72920 (Accessing a private constant using constant() creates @@ -88,6 +83,8 @@ PHP NEWS should match). (cmb). . Add ReflectionNamedType::getName(). This method should be used instead of ReflectionType::__toString() + . Prepend \ for class names and ? for nullable types returned from + ReflectionType::__toString(). (Trowski) - Session: . Implemented RFC: Session ID without hashing. (Yasuo) diff --git a/UPGRADING b/UPGRADING index 71cc31296699c..410a0f25732a6 100644 --- a/UPGRADING +++ b/UPGRADING @@ -124,6 +124,9 @@ PHP 7.1 UPGRADE NOTES . The behavior of ReflectionMethod::invoke() and ::invokeArgs() has been aligned, what causes slightly different behavior than before for some pathological cases. + . ReflectionType::__toString() will now return the type name with a leading + "?" if it is nullable. To retrieve the type name without leading "?" the new + ReflectionNamedType::getName() method can be used. ======================================== 2. New Features @@ -305,9 +308,6 @@ PHP 7.1 UPGRADE NOTES - Reflection: . Failure to retrieve a reflection object or retrieve an object property will now throw an instance of Error instead of resulting in a fatal error. - . ReflectionNamedType will be returned from ReflectionParameter::getType() - and ReflectionFunctionAbstract::getReturnType(). This class includes a - getName() method that returns the type name as a string. - Session: . Custom session handlers that do not return strings for session IDs will diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index a164cbda721ba..cf861b65d81b0 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3028,13 +3028,23 @@ ZEND_METHOD(reflection_type, __toString) { reflection_object *intern; type_reference *param; + zend_string *str; if (zend_parse_parameters_none() == FAILURE) { return; } GET_REFLECTION_OBJECT_PTR(param); - RETURN_STR(reflection_type_name(param)); + str = reflection_type_name(param); + + if (param->arg_info->allow_null) { + size_t orig_len = ZSTR_LEN(str); + str = zend_string_extend(str, orig_len + 1, 0); + memmove(ZSTR_VAL(str) + 1, ZSTR_VAL(str), orig_len + 1); + ZSTR_VAL(str)[0] = '?'; + } + + RETURN_STR(str); } /* }}} */ diff --git a/ext/reflection/tests/ReflectionNamedType.phpt b/ext/reflection/tests/ReflectionNamedType.phpt index 123e09d51c982..1e8f55ac9d8a5 100644 --- a/ext/reflection/tests/ReflectionNamedType.phpt +++ b/ext/reflection/tests/ReflectionNamedType.phpt @@ -32,10 +32,10 @@ var_dump((string) $return); ?> --EXPECTF-- string(11) "Traversable" -string(11) "Traversable" -string(6) "string" +string(12) "?Traversable" string(6) "string" +string(7) "?string" string(4) "Test" +string(5) "?Test" string(4) "Test" -string(4) "Test" -string(4) "Test" +string(5) "?Test" diff --git a/ext/reflection/tests/ReflectionType_001.phpt b/ext/reflection/tests/ReflectionType_001.phpt index f764cf1519d92..ee2ebb64dd63c 100644 --- a/ext/reflection/tests/ReflectionType_001.phpt +++ b/ext/reflection/tests/ReflectionType_001.phpt @@ -73,6 +73,8 @@ foreach ([ var_dump((string)$ra); } } + +?> --EXPECT-- *** functions ** Function 0 - Parameter 0 @@ -94,7 +96,7 @@ string(8) "callable" bool(true) bool(true) bool(false) -string(8) "stdClass" +string(9) "?stdClass" ** Function 0 - Parameter 4 bool(false) ** Function 0 - Parameter 5