Skip to content

Commit

Permalink
fix: collect() returns collection which can have array-key as key type
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Feb 28, 2022
1 parent b3413da commit b0a50df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ReturnTypes/Helpers/CollectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class CollectExtension implements DynamicFunctionReturnTypeExtension
Expand All @@ -38,7 +40,7 @@ public function getTypeFromFunctionCall(
Scope $scope
): Type {
if (count($functionCall->getArgs()) < 1) {
return new GenericObjectType(Collection::class, [new IntegerType(), new MixedType()]);
return new GenericObjectType(Collection::class, [new BenevolentUnionType([new IntegerType(), new StringType()]), new MixedType()]);
}

$valueType = $scope->getType($functionCall->getArgs()[0]->value);
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/data/collection-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Collection;
use function PHPStan\Testing\assertType;

assertType('Illuminate\Support\Collection<int, mixed>', collect());
assertType('Illuminate\Support\Collection<(int|string), mixed>', collect());
assertType('Illuminate\Support\Collection<0, 1>', collect(1));
assertType('Illuminate\Support\Collection<0, \'foo\'>', collect('foo'));
assertType('Illuminate\Support\Collection<0, 3.14>', collect(3.14));
Expand Down

0 comments on commit b0a50df

Please sign in to comment.