xref: /PHP-8.0/ext/reflection/tests/bug80190.phpt (revision dee80249)
1--TEST--
2Bug #80190: ReflectionMethod::getReturnType() does not handle static as part of union type
3--FILE--
4<?php
5
6class 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
25foreach ((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--
49C::a()
50    $method->getReturnType() returns ReflectionNamedType
51    $method->getReturnType()->__toString() returns self
52
53C::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
59C::c()
60    $method->getReturnType() returns ReflectionNamedType
61    $method->getReturnType()->__toString() returns static
62
63C::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)
68