rectorphp / type-perfect

Next level type declaration check PHPStan rules
https://getrector.com/blog/introducing-type-perfect-for-extra-safety
MIT License
74 stars 4 forks source link

NarrowPublicClassMethodParamTypeRule: wrong error when method is called recusively #36

Open scollovati opened 3 months ago

scollovati commented 3 months ago

Example:

public function myMethod(string $Id, int $recursive_round = 1): ?array
{
    [...]

    return self::myMethod($Id, 2);
}

Error wrongly reported: Parameters should have "string" types as the only types passed to this method

Actually, the error message is misleading (reported here: https://github.com/rectorphp/type-perfect/issues/35#issuecomment-2217169072) but also wrong. The parameter should not be removed from the method signature.

TheoD02 commented 3 months ago

Hi,

I'm encountering a similar issue, but in a different context. Here's the situation 😄

Method Definition

public function authenticate(string|false $code = false): ?string

Reported Error

Parameters should have "string|false" types as the only types passed to this method

In my project, I have only two calls to this method:

$accessToken = $this->service->oauth()->authenticate($code);
// and
$accessToken = $this->service->oauth()->authenticate();

Expected Behavior

I expect no error because false is the default value.

Temporary Solution

To resolve this issue for now, I am explicitly setting the default value to false:

$accessToken = $this->deezerClient->oauth()->authenticate(false);