]> granicus.if.org Git - php/commitdiff
Deprecate ReflectionType::__toString()
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 5 Jun 2019 13:33:52 +0000 (15:33 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 5 Jun 2019 13:33:52 +0000 (15:33 +0200)
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.

UPGRADING
Zend/tests/object_types/return_type_reflection.phpt
Zend/tests/object_types/type_hint_reflection.phpt
Zend/tests/type_declarations/typed_properties_018.phpt
ext/reflection/php_reflection.c
ext/reflection/tests/ReflectionNamedType.phpt
ext/reflection/tests/ReflectionType_001.phpt
ext/reflection/tests/bug72661.phpt

index 49e4edf40e850aaf50d8cb00ce9019fc2677b99f..f8817889fa972a55f9d45c78173d0f1cc35c1231 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -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
 ========================================
index bcb918287d167e4bdad6eef461157c9a848d36f3..ac01d90894a20448254e60d12f2994d082405f56 100644 (file)
@@ -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"
index 6a4f27d8c948f427c5bdb28d820f73687cf1c39f..b530e6dc0467b76762ff15013260825161cbbc66 100644 (file)
@@ -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"
index 097b39b232bf144ceaedbf7bc4562fffa775644b..f3ac4504b1faaae62fd98c6ab7d2257bf217d895 100644 (file)
@@ -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"
index 4623efb3cf970354e9bc2ed66b7b82e047dffe21..ab519251806de1b10eb68feb9f4dda6b78bd5f28 100644 (file)
@@ -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
 };
 
index a40d4066ec3292ac96787b7669eeb2124c3243ac..afb592127c3863b4afb8ffe69ec80dd5349a9622 100644 (file)
@@ -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"
index e47a9615ba56f4cdb4d2e98c44e9526f7dafdef5..1eb521317ceba5d37df86ad98f1f413a8f36af2a 100644 (file)
@@ -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());
 }
 
index b1cb764beb62ad4a8df12d5f047d29e4e6e00aae..46ba048078b75cf773f2123f12e5704f0d5f6e6c 100644 (file)
@@ -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"