Skip to content

Commit

Permalink
Deprecate ReflectionType::__toString()
Browse files Browse the repository at this point in the history
We weren't able to do this in 7.1 because the deprecation notice
may be converted to an exception and __toString() can't throw,
which means that it ultimately become a fatal error. This issue
is resolved now, so we can mark the method as deprecated.
  • Loading branch information
nikic committed Jun 5, 2019
1 parent a31f464 commit b964298
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions UPGRADING
Expand Up @@ -306,6 +306,12 @@ PHP 7.4 UPGRADE NOTES
. ldap_control_paged_result_response and ldap_control_paged_result are
deprecated. Pagination controls can be sent along with ldap_search instead.

- Reflection:
. Calls to ReflectionType::__toString() now generate a deprecation notice.
This method has been deprecated in favor of ReflectionNamedType::getName()
in the documentation since PHP 7.1, but did not throw a deprecation notice
for technical reasons.

========================================
5. Changed Functions
========================================
Expand Down
6 changes: 3 additions & 3 deletions Zend/tests/object_types/return_type_reflection.phpt
Expand Up @@ -14,13 +14,13 @@ class Two implements One {
function a() : object {}

$returnTypeOne = (new ReflectionClass(One::class))->getMethod('a')->getReturnType();
var_dump($returnTypeOne->isBuiltin(), (string)$returnTypeOne);
var_dump($returnTypeOne->isBuiltin(), $returnTypeOne->getName());

$returnTypeTwo = (new ReflectionClass(Two::class))->getMethod('a')->getReturnType();
var_dump($returnTypeTwo->isBuiltin(), (string)$returnTypeTwo);
var_dump($returnTypeTwo->isBuiltin(), $returnTypeTwo->getName());

$returnTypea = (new ReflectionFunction('a'))->getReturnType();
var_dump($returnTypea->isBuiltin(), (string)$returnTypea);
var_dump($returnTypea->isBuiltin(), $returnTypea->getName());
--EXPECT--
bool(true)
string(6) "object"
Expand Down
6 changes: 3 additions & 3 deletions Zend/tests/object_types/type_hint_reflection.phpt
Expand Up @@ -14,13 +14,13 @@ class Two implements One {
function a(object $obj) {}

$typeHintOne = (new ReflectionClass(One::class))->getMethod('a')->getParameters()[0]->getType();
var_dump($typeHintOne->isBuiltin(), (string)$typeHintOne);
var_dump($typeHintOne->isBuiltin(), $typeHintOne->getName());

$typeHintTwo = (new ReflectionClass(Two::class))->getMethod('a')->getParameters()[0]->getType();
var_dump($typeHintTwo->isBuiltin(), (string)$typeHintTwo);
var_dump($typeHintTwo->isBuiltin(), $typeHintTwo->getName());

$typeHinta = (new ReflectionFunction('a'))->getParameters()[0]->getType();
var_dump($typeHinta->isBuiltin(), (string)$typeHinta);
var_dump($typeHinta->isBuiltin(), $typeHinta->getName());
--EXPECT--
bool(true)
string(6) "object"
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/type_declarations/typed_properties_018.phpt
Expand Up @@ -11,7 +11,7 @@ $reflector = new ReflectionClass(Foo::class);

$prop = $reflector->getProperty("qux");

var_dump((string) $prop->getType());
var_dump($prop->getType()->getName());
?>
--EXPECT--
string(3) "int"
5 changes: 1 addition & 4 deletions ext/reflection/php_reflection.c
Expand Up @@ -6665,10 +6665,7 @@ 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, isBuiltin, arginfo_reflection__void, 0)
/* ReflectionType::__toString() is deprecated, but we currently do not mark it as such
* due to bad interaction with the PHPUnit error handler and exceptions in __toString().
* See PR2137. */
ZEND_ME(reflection_type, __toString, arginfo_reflection__void, 0)
ZEND_ME(reflection_type, __toString, arginfo_reflection__void, ZEND_ACC_DEPRECATED)
PHP_FE_END
};

Expand Down
10 changes: 9 additions & 1 deletion ext/reflection/tests/ReflectionNamedType.phpt
Expand Up @@ -30,12 +30,20 @@ var_dump($return->getName());
var_dump((string) $return);

?>
--EXPECT--
--EXPECTF--
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(6) "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(4) "Test"
2 changes: 1 addition & 1 deletion ext/reflection/tests/ReflectionType_001.phpt
Expand Up @@ -91,7 +91,7 @@ $reflector = new ReflectionClass(PropTypeTest::class);
foreach ($reflector->getProperties() as $name => $property) {
if ($property->hasType()) {
printf("public %s $%s;\n",
$property->getType(), $property->getName());
$property->getType()->getName(), $property->getName());
} else printf("public $%s;\n", $property->getName());
}

Expand Down
3 changes: 2 additions & 1 deletion ext/reflection/tests/bug72661.phpt
Expand Up @@ -6,5 +6,6 @@ function test(iterable $arg) { }

var_dump((string)(new ReflectionParameter("test", 0))->getType());
?>
--EXPECT--
--EXPECTF--
Deprecated: Function ReflectionType::__toString() is deprecated in %s on line %d
string(8) "iterable"

0 comments on commit b964298

Please sign in to comment.