summaryrefslogtreecommitdiff
path: root/ext/reflection/tests/bug80190.phpt
blob: 16f2fbba4272970e52e2e70c93a67fa922bcc39c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--TEST--
Bug #80190: ReflectionMethod::getReturnType() does not handle static as part of union type
--FILE--
<?php

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) . "($type)";
        }
        
        print join(', ', $types) . PHP_EOL;
    }

    print PHP_EOL;
}

?>
--EXPECT--
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(stdClass), ReflectionNamedType(self)

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 2 element(s)
    type(s) in union: ReflectionNamedType(stdClass), ReflectionNamedType(static)