rectorphp / type-perfect

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

callable reported when more specific Closure is used #9

Closed janedbal closed 2 months ago

janedbal commented 2 months ago

Using narrow_param: true, method looks like this:

public function map(Closure $mapper): array

And get wrongly reported with wider suggestion:

Parameters should have "callable" types as the only types passed to this method
samsonasik commented 2 months ago

@janedbal could you give more precise code when it shown the error? thank you.

janedbal commented 2 months ago

The map method is called only with Closures. But it gets reported to be changed to callable, which is more wider type than used.

$object->map(function () {});
samsonasik commented 2 months ago

It seems between Closure and callable make flip flop in case of this check removed:

https://github.com/rectorphp/type-perfect/blob/df83404dda3364cae1b4f5251420f95ef20fa493/src/Printer/CollectorMetadataPrinter.php#L177-L179

then, create a method with callable method:

final class SkipCallable
{
    public function map(callable $mapper): array
    {
        return [];
    }

    public function run()
    {
        $this->map(function () {});
    }
}

then become error:

+'09: Parameters should have "Closure" types as the only types passed to this method