[php-src] Issue #15196: BackedEnum::from($value) ReflectionType is inconsistent with the actual type at runtime
| From: | vudaltsov | Date: | Thu, 01 Aug 2024 12:56:06 +0000 |
| Subject: | [php-src] Issue #15196: BackedEnum::from($value) ReflectionType is inconsistent with the actual type at runtime | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247419@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15196
Author: vudaltsov
### Description
The following code:
```php
<?php
declare(strict_types=1);
enum MyBackedEnum: int
{
case One = 1;
}
try {
MyBackedEnum::from('1');
} catch (Throwable $exception) {
echo $exception->getMessage(), PHP_EOL;
}
echo (new ReflectionProperty(MyBackedEnum::class, 'value'))->getType(), PHP_EOL;
echo (new ReflectionParameter([MyBackedEnum::class, 'from'],
'value'))->getType(), PHP_EOL;
echo (new ReflectionParameter([MyBackedEnum::class, 'tryFrom'],
'value'))->getType(), PHP_EOL;
```
Resulted in this output:
```
MyBackedEnum::from(): Argument #1 ($value) must be of type int, string given
int
string|int
string|int
```
But I expected this output instead:
```
MyBackedEnum::from(): Argument #1 ($value) must be of type int, string given
int
int
int
```
https://3v4l.org/WkLXe
### PHP Version
PHP 8.2
### Operating System
_No response_