Skip to content

Document ArgumentCountError for internal functions #2450

@enumag

Description

@enumag

Description

The following code doesn't report any issue because PHP generally doesn't care about extra arguments being passed to a method:

class Y {
    public static function from(string $value): self
    {
        return new self();
    }
}

Y::from('name', 1);

However the following code:

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)
php/php-src#1 {main}
  thrown in /in/aKdjL on line 7

Process exited with code 255.

This is a really nasty behavior, especially because it is neither documented nor detectable with 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions