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