Skip to content

Collection transform phpdoc is too restrictive #46487

@neodc

Description

@neodc
  • Laravel Version: 10.3.3
  • PHP Version: 8.1.11
  • Database Driver & Version: N/A

Description:

The transform method phpdoc indicate that the return type of the callable should be the same as the input one, but transform should (like map) allow to change the type of the items.

It make it so that Phpstan think that valid code are not valid.

Steps To Reproduce:

Here's a basic example that trigger an error in phpstan:

$test = collect([1, 2, 3]); $test->transform(fn(int $input): string => (string)$input);

When I run Phpstan on level 8 (I did not try it on lower level) I get this error:

Parameter #1 $callback of method Illuminate\Support\Collection<int,int>::transform() expects callable(int, int): int, Closure(int): numeric-string given. 

The same code with transform replace by map do not trigger any error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions