bug80190.phpt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --TEST--
  2. Bug #80190: ReflectionMethod::getReturnType() does not handle static as part of union type
  3. --FILE--
  4. <?php
  5. class C
  6. {
  7. public function a(): self
  8. {
  9. }
  10. public function b(): stdClass|self
  11. {
  12. }
  13. public function c(): static
  14. {
  15. }
  16. public function d(): stdClass|static
  17. {
  18. }
  19. }
  20. foreach ((new ReflectionClass(C::class))->getMethods() as $method) {
  21. print $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()' . PHP_EOL;
  22. print ' $method->getReturnType() returns ' . get_class($method->getReturnType()) . PHP_EOL;
  23. print ' $method->getReturnType()->__toString() returns ' . $method->getReturnType() . PHP_EOL;
  24. if ($method->getReturnType() instanceof ReflectionUnionType) {
  25. print ' $method->getReturnType()->getTypes() returns an array with ' . count($method->getReturnType()->getTypes()) . ' element(s)' . PHP_EOL;
  26. print ' type(s) in union: ';
  27. $types = [];
  28. foreach ($method->getReturnType()->getTypes() as $type) {
  29. $types[] = get_class($type) . "($type)";
  30. }
  31. print join(', ', $types) . PHP_EOL;
  32. }
  33. print PHP_EOL;
  34. }
  35. ?>
  36. --EXPECT--
  37. C::a()
  38. $method->getReturnType() returns ReflectionNamedType
  39. $method->getReturnType()->__toString() returns self
  40. C::b()
  41. $method->getReturnType() returns ReflectionUnionType
  42. $method->getReturnType()->__toString() returns stdClass|self
  43. $method->getReturnType()->getTypes() returns an array with 2 element(s)
  44. type(s) in union: ReflectionNamedType(stdClass), ReflectionNamedType(self)
  45. C::c()
  46. $method->getReturnType() returns ReflectionNamedType
  47. $method->getReturnType()->__toString() returns static
  48. C::d()
  49. $method->getReturnType() returns ReflectionUnionType
  50. $method->getReturnType()->__toString() returns stdClass|static
  51. $method->getReturnType()->getTypes() returns an array with 2 element(s)
  52. type(s) in union: ReflectionNamedType(stdClass), ReflectionNamedType(static)