[php-src] Issue #11140: Unexpected ArgumentCountError
| From: | enumag | Date: | Thu, 27 Apr 2023 08:03:55 +0000 |
| Subject: | [php-src] Issue #11140: Unexpected ArgumentCountError | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244282@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11140
Author: enumag
### Description
The following code doesn't report any issue because PHP generally doesn't care about extra
arguments being passed to a method:
```php
class Y {
public static function from(string $value): self
{
return new self();
}
}
Y::from('name', 1);
```
However the following code:
```php
enum X: string {
case NAME = 'name';
}
X::from('name', 1);
```
causes a fatal error:
```
Fatal error: Uncaught ArgumentCountError: X::from() expects exactly 1 argument, 2 given in
/in/aKdjL:7
Stack trace:
#0 /in/aKdjL(7): X::from('name', 1)
#1 {main}
thrown in /in/aKdjL on line 7
Process exited with code 255.
```
This is a really nasty behavior, especially because is neither documented nor detectable through
reflection: https://3v4l.org/6XPsW#v8.2.5
It matters especially when using such function as a first-class-callable. I stumbled upon it with
this code: https://3v4l.org/YWSEo#v8.2.5
The worst thing about this is that due to this behavior being undocumented and undetectable there is
no way for tools like PHPStan to report such problem either.
I don't even know if
BackedEnum::from() is the only such function or if there are
any others.
### PHP Version
8.2.5
### Operating System
any