Skip to content

Commit

Permalink
Fix and undeprecate ReflectionType::__toString()
Browse files Browse the repository at this point in the history
Add deprecated _ZendTestClass::__toString() method to preserve
an existing test.

ReflectionType::__toString() will now return a complete
representation of the type, as it should have originally. Users
that relied on nullability being absent should have been pushed
to ReflectionNamedType::getName() by the deprecation of
ReflectionType::__toString() in PHP 7.1 / PHP 7.4.
  • Loading branch information
nikic committed Oct 22, 2019
1 parent 1faea28 commit 530a8a3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ PHP 8.0 UPGRADE NOTES
ReflectionFunction::invoke($arg = null, ...$args)
ReflectionMethod::invoke($object, $arg = null, ...$args)

. The ReflectionType::__toString() method will now return a complete debug
representation of the type, and is no longer deprecated. In particular the
result will include a nullability indicator for nullable types. The format
of the return value is not stable and may change between PHP versions.

- Socket:
. The deprecated AI_IDN_ALLOW_UNASSIGNED and AI_IDN_USE_STD3_ASCII_RULES
flags for socket_addrinfo_lookup() have been removed.
Expand Down
17 changes: 5 additions & 12 deletions Zend/tests/bug78239.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Bug #78239: Deprecation notice during string conversion converted to exception hangs
--SKIPIF--
<?php if (!extension_loaded("zend-test")) die("skip requires zend-test extension"); ?>
--FILE--
<?php
function handleError($level, $message, $file = '', $line = 0, $context = [])
Expand All @@ -9,21 +11,12 @@ function handleError($level, $message, $file = '', $line = 0, $context = [])

set_error_handler('handleError');

class A
{

public function abc(): bool
{
return false;
}
}

$r = new ReflectionMethod("A", "abc");
(string)$r->getReturnType() ?: "";
$r = new _ZendTestClass;
(string)$r ?: "";

?>
--EXPECTF--
Fatal error: Uncaught ErrorException: Function ReflectionType::__toString() is deprecated in %s:%d
Fatal error: Uncaught ErrorException: Function _ZendTestClass::__toString() is deprecated in %s:%d
Stack trace:
#0 %s(%d): handleError(%s)
#1 {main}
Expand Down
4 changes: 2 additions & 2 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ ZEND_METHOD(reflection_type, __toString)
}
GET_REFLECTION_OBJECT_PTR(param);

RETURN_STR(zend_type_to_string(ZEND_TYPE_WITHOUT_NULL(param->type)));
RETURN_STR(zend_type_to_string(param->type));
}
/* }}} */

Expand Down Expand Up @@ -6621,7 +6621,7 @@ static const zend_function_entry reflection_parameter_functions[] = {
static const zend_function_entry reflection_type_functions[] = {
ZEND_ME(reflection, __clone, arginfo_reflection__void, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
ZEND_ME(reflection_type, allowsNull, arginfo_reflection__void, 0)
ZEND_ME(reflection_type, __toString, arginfo_reflection__void, ZEND_ACC_DEPRECATED)
ZEND_ME(reflection_type, __toString, arginfo_reflection__void, 0)
PHP_FE_END
};

Expand Down
18 changes: 5 additions & 13 deletions ext/reflection/tests/ReflectionNamedType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ var_dump($return->getName());
var_dump((string) $return);

?>
--EXPECTF--
--EXPECT--
string(11) "Traversable"

Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
string(11) "Traversable"
string(6) "string"

Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
string(12) "?Traversable"
string(6) "string"
string(7) "?string"
string(4) "Test"

Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
string(4) "Test"
string(4) "Test"

Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
string(5) "?Test"
string(4) "Test"
string(5) "?Test"
3 changes: 1 addition & 2 deletions ext/reflection/tests/bug72661.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ function test(iterable $arg) { }

var_dump((string)(new ReflectionParameter("test", 0))->getType());
?>
--EXPECTF--
Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
--EXPECT--
string(8) "iterable"
14 changes: 12 additions & 2 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,21 @@ static zend_function *zend_test_class_static_method_get(zend_class_entry *ce, ze
}
/* }}} */

static ZEND_METHOD(_ZendTestClass, __toString) /* {{{ */ {
RETURN_EMPTY_STRING();
}
/* }}} */

static ZEND_METHOD(_ZendTestTrait, testMethod) /* {{{ */ {
RETURN_TRUE;
}
/* }}} */

static const zend_function_entry zend_test_class_methods[] = {
ZEND_ME(_ZendTestClass, __toString, NULL, ZEND_ACC_DEPRECATED)
ZEND_FE_END
};

static const zend_function_entry zend_test_trait_methods[] = {
ZEND_ME(_ZendTestTrait, testMethod, NULL, ZEND_ACC_PUBLIC)
ZEND_FE_END
Expand All @@ -204,8 +214,8 @@ PHP_MINIT_FUNCTION(zend_test)
INIT_CLASS_ENTRY(class_entry, "_ZendTestInterface", NULL);
zend_test_interface = zend_register_internal_interface(&class_entry);
zend_declare_class_constant_long(zend_test_interface, ZEND_STRL("DUMMY"), 0);
INIT_CLASS_ENTRY(class_entry, "_ZendTestClass", NULL);
zend_test_class = zend_register_internal_class_ex(&class_entry, NULL);
INIT_CLASS_ENTRY(class_entry, "_ZendTestClass", zend_test_class_methods);
zend_test_class = zend_register_internal_class(&class_entry);
zend_class_implements(zend_test_class, 1, zend_test_interface);
zend_test_class->create_object = zend_test_class_new;
zend_test_class->get_static_method = zend_test_class_static_method_get;
Expand Down

0 comments on commit 530a8a3

Please sign in to comment.