Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ArrayChunkFunctionReturnTypeExtension
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Accessory\NonEmptyArrayType; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeTraverser; | ||
use PHPStan\Type\UnionType; | ||
use function count; | ||
|
||
final class ArrayChunkFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'array_chunk'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
{ | ||
if (count($functionCall->getArgs()) < 2) { | ||
return null; | ||
} | ||
|
||
$arrayType = $scope->getType($functionCall->getArgs()[0]->value); | ||
$lengthType = $scope->getType($functionCall->getArgs()[1]->value); | ||
$preserveKeysType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : null; | ||
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : false; | ||
|
||
if (!$arrayType->isArray()->yes() || !$lengthType instanceof ConstantIntegerType || $lengthType->getValue() < 1) { | ||
return null; | ||
} | ||
|
||
return TypeTraverser::map($arrayType, static function (Type $type, callable $traverse) use ($lengthType, $preserveKeys): Type { | ||
if ($type instanceof UnionType || $type instanceof IntersectionType) { | ||
return $traverse($type); | ||
} | ||
|
||
if ($type instanceof ConstantArrayType) { | ||
return $type->chunk($lengthType->getValue(), $preserveKeys); | ||
} | ||
|
||
$chunkType = $preserveKeys ? $type : new ArrayType(new IntegerType(), $type->getIterableValueType()); | ||
$chunkType = TypeCombinator::intersect($chunkType, new NonEmptyArrayType()); | ||
|
||
$resultType = new ArrayType(new IntegerType(), $chunkType); | ||
if ($type->isIterableAtLeastOnce()->yes()) { | ||
$resultType = TypeCombinator::intersect($type, new NonEmptyArrayType()); | ||
} | ||
return $resultType; | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ArrayChunk; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function generalArrays(array $arr): void | ||
{ | ||
/** @var mixed[] $arr */ | ||
assertType('array<int, non-empty-array<int, mixed>>', array_chunk($arr, 2)); | ||
assertType('array<int, non-empty-array>', array_chunk($arr, 2, true)); | ||
|
||
/** @var array<string, int> $arr */ | ||
assertType('array<int, non-empty-array<int, int>>', array_chunk($arr, 2)); | ||
assertType('array<int, non-empty-array<string, int>>', array_chunk($arr, 2, true)); | ||
|
||
/** @var non-empty-array<int|string, bool> $arr */ | ||
assertType('non-empty-array<int, non-empty-array<int, bool>>', array_chunk($arr, 1)); | ||
assertType('non-empty-array<int, non-empty-array<int|string, bool>>', array_chunk($arr, 1, true)); | ||
} | ||
|
||
|
||
public function constantArrays(array $arr): void | ||
{ | ||
/** @var array{a: 0, 17: 1, b: 2} $arr */ | ||
assertType('array{array{0, 1}, array{2}}', array_chunk($arr, 2)); | ||
assertType('array{array{a: 0, 17: 1}, array{b: 2}}', array_chunk($arr, 2, true)); | ||
assertType('array{array{0}, array{1}, array{2}}', array_chunk($arr, 1)); | ||
assertType('array{array{a: 0}, array{17: 1}, array{b: 2}}', array_chunk($arr, 1, true)); | ||
} | ||
|
||
public function constantArraysWithOptionalKeys(array $arr): void | ||
{ | ||
/** @var array{a: 0, b?: 1, c: 2} $arr */ | ||
assertType('array{array{a: 0, b?: 1, c?: 2}, array{c?: 2}}', array_chunk($arr, 2, true)); | ||
assertType('array{array{a: 0, b?: 1, c: 2}}', array_chunk($arr, 3, true)); | ||
assertType('array{array{a: 0}, array{b?: 1, c?: 2}, array{c?: 2}}', array_chunk($arr, 1, true)); | ||
|
||
/** @var array{a?: 0, b?: 1, c?: 2} $arr */ | ||
assertType('array{array{a?: 0, b?: 1, c?: 2}, array{c?: 2}}', array_chunk($arr, 2, true)); | ||
} | ||
|
||
} |