Bug #80190 [NEW]: ReflectionMethod::getReturnType() does not handle static as part of union type

From: Date: Tue, 06 Oct 2020 06:11:30 +0000
Subject: Bug #80190 [NEW]: ReflectionMethod::getReturnType() does not handle static as part of union type
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229414@lists.php.net to get a copy of this message
From: sebastian Operating system: Irrelevant PHP version: 8.0Git-2020-10-06 (Git) Package: Reflection related Bug Type: Bug Bug description:ReflectionMethod::getReturnType() does not handle static as part of union type Description: ------------ Consider this class: class C { public function m(): stdClass|static { } } ReflectionMethod::getReturnType() returns for C::m() a ReflectionUnionType that contains only a ReflectionNamedType object for "stdClass" but no information about "static". This blocks PHPUnit's support for test doubles of objects with methods that declare a return type with a union that includes "static" (https://github.com/sebastianbergmann/phpunit/issues/4480, https://github.com/sebastianbergmann/type/issues/14). Test script: --------------- <?php declare(strict_types=1); class C { public function a(): self { } public function b(): stdClass|self { } public function c(): static { } public function d(): stdClass|static { } } foreach ((new ReflectionClass(C::class))->getMethods() as $method) { print $method->getDeclaringClass()->getName() . '::' . $method->getName() . '()' . PHP_EOL; print ' $method->getReturnType() returns ' . get_class($method->getReturnType()) . PHP_EOL; print ' $method->getReturnType()->__toString() returns ' . $method->getReturnType() . PHP_EOL; if ($method->getReturnType() instanceof ReflectionUnionType) { print ' $method->getReturnType()->getTypes() returns an array with ' . count($method->getReturnType()->getTypes()) . ' element(s)' . PHP_EOL; print ' type(s) in union: '; $types = []; foreach ($method->getReturnType()->getTypes() as $type) { $types[] = get_class($type); } print join(', ', $types) . PHP_EOL; } print PHP_EOL; } Expected result: ---------------- C::a() $method->getReturnType() returns ReflectionNamedType $method->getReturnType()->__toString() returns self C::b() $method->getReturnType() returns ReflectionUnionType $method->getReturnType()->__toString() returns stdClass|self $method->getReturnType()->getTypes() returns an array with 2 element(s) type(s) in union: ReflectionNamedType, ReflectionNamedType C::c() $method->getReturnType() returns ReflectionNamedType $method->getReturnType()->__toString() returns static C::d() $method->getReturnType() returns ReflectionUnionType $method->getReturnType()->__toString() returns stdClass|static $method->getReturnType()->getTypes() returns an array with 1 element(s) type(s) in union: ReflectionNamedType, SOMETHING FOR STATIC Actual result: -------------- C::a() $method->getReturnType() returns ReflectionNamedType $method->getReturnType()->__toString() returns self C::b() $method->getReturnType() returns ReflectionUnionType $method->getReturnType()->__toString() returns stdClass|self $method->getReturnType()->getTypes() returns an array with 2 element(s) type(s) in union: ReflectionNamedType, ReflectionNamedType C::c() $method->getReturnType() returns ReflectionNamedType $method->getReturnType()->__toString() returns static C::d() $method->getReturnType() returns ReflectionUnionType $method->getReturnType()->__toString() returns stdClass|static $method->getReturnType()->getTypes() returns an array with 1 element(s) type(s) in union: ReflectionNamedType -- Edit bug report at https://bugs.php.net/bug.php?id=80190&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=80190&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=80190&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=80190&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=80190&r=needscript Try newer version: https://bugs.php.net/fix.php?id=80190&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=80190&r=support Expected behavior: https://bugs.php.net/fix.php?id=80190&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=80190&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=80190&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=80190&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=80190&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=80190&r=dst IIS Stability: https://bugs.php.net/fix.php?id=80190&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=80190&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=80190&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=80190&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=80190&r=mysqlcfg

« previous php.bugs (#229414) next »