Bug #80190 [Opn->Csd]: ReflectionMethod::getReturnType() does not handle static as part of union type

From: Date: Tue, 06 Oct 2020 08:21:26 +0000
Subject: Bug #80190 [Opn->Csd]: ReflectionMethod::getReturnType() does not handle static as part of union type
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229417@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80190&edit=1 ID: 80190 Updated by: nikic@php.net Reported by: sebastian@php.net Summary: ReflectionMethod::getReturnType() does not handle static as part of union type -Status: Open +Status: Closed Type: Bug Package: Reflection related Operating System: Irrelevant PHP Version: 8.0Git-2020-10-06 (Git) Block user comment: N Private report: N New Comment: Automatic comment on behalf of nikita.ppv@gmail.com Revision: http://git.php.net/?p=php-src.git;a=commit;h=dee802498e25e22afb35e550dea1f7b3a8aa0129 Log: Fixed bug #80190 Previous Comments: ------------------------------------------------------------------------ [2020-10-06 06:46:22] sebastian@php.net Unfortunately, our arcane issue tracker does not let me edit my original report. If if did, I would make the following edits: * Change "$method->getReturnType()->getTypes() returns an array with 1 element(s)" to "$method->getReturnType()->getTypes() returns an array with 2 element(s)" for C::d() in the "Expected result" section * Change "ReflectionNamedType, SOMETHING FOR STATIC" to "ReflectionNamedType, ReflectionNamedType" for C::d() in the "Expected result" section I think it only makes sense to represent "static" the same way "self" is represented. ------------------------------------------------------------------------ [2020-10-06 06:11:30] sebastian@php.net 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 this bug report at https://bugs.php.net/bug.php?id=80190&edit=1

« previous php.bugs (#229417) next »