|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-10-06 06:46 UTC] sebastian@php.net
[2020-10-06 08:21 UTC] nikic@php.net
[2020-10-06 08:21 UTC] nikic@php.net
-Status: Open
+Status: Closed
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 01:00:01 2025 UTC |
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