Skip to content

Commit dee8024

Browse files
committed
Fixed bug #80190
1 parent d9dce83 commit dee8024

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
. Fixed bug #80184 (Complex expression in while / if statements resolves to
1111
false incorrectly). (Nikita)
1212

13+
- Reflection:
14+
. Fixed bug #80190 (ReflectionMethod::getReturnType() does not handle static
15+
as part of union type). (Nikita)
16+
1317
- SPL:
1418
. Fixed bug #65387 (Circular references in SPL iterators are not garbage
1519
collected). (Nikita)

ext/reflection/php_reflection.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,9 @@ ZEND_METHOD(ReflectionUnionType, getTypes)
29522952

29532953
type_mask = ZEND_TYPE_PURE_MASK(param->type);
29542954
ZEND_ASSERT(!(type_mask & MAY_BE_VOID));
2955+
if (type_mask & MAY_BE_STATIC) {
2956+
append_type_mask(return_value, MAY_BE_STATIC);
2957+
}
29552958
if (type_mask & MAY_BE_CALLABLE) {
29562959
append_type_mask(return_value, MAY_BE_CALLABLE);
29572960
}

ext/reflection/tests/bug80190.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
Bug #80190: ReflectionMethod::getReturnType() does not handle static as part of union type
3+
--FILE--
4+
<?php
5+
6+
class 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+
25+
foreach ((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--
49+
C::a()
50+
$method->getReturnType() returns ReflectionNamedType
51+
$method->getReturnType()->__toString() returns self
52+
53+
C::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+
59+
C::c()
60+
$method->getReturnType() returns ReflectionNamedType
61+
$method->getReturnType()->__toString() returns static
62+
63+
C::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)

0 commit comments

Comments
 (0)