[php-src] Issue #14501: Union Type Not Reported by ReflectionProperty for Simple Union Types
| From: | Zykaris | Date: | Fri, 07 Jun 2024 19:52:54 +0000 |
| Subject: | [php-src] Issue #14501: Union Type Not Reported by ReflectionProperty for Simple Union Types | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247061@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14501
Author: Zykaris
### Description
### Overview
Recently, I have been using the ReflectionClass to enable significant code reuse in an otherwise
repetitive area. But I just stumbled across an issue/inconsistency, in that a two-piece union type
of null + anything, is not seen as a union type by ReflectionProperty.
The following code:
```php
<?php
class Box
{
public int|null $size;
}
$reflection = new ReflectionClass(Box::class);
$type = $reflection->getProperty("size")->getType();
echo $type::class . "\n";
```
Resulted in this output:
```
ReflectionNamedType
```
But I expected this output instead:
```
ReflectionUnionType
```
However, if the type of $size is changed to "int|string|null", then ReflectionUnionType is
properly detected/echoed, and the individual type names can be checked.
This is rather frustrating, as I don't know of another way to dynamically determine whether a
type-hinted property can accept null values (well, without catching errors, but that seems like a
poor way to work).
### PHP Version
PHP 8.3.8
### Operating System
Windows 11