Skip to content

Add Type::chunkArray() #3408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1283,11 +1283,6 @@ parameters:
count: 4
path: src/Type/ObjectWithoutClassType.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
count: 1
path: src/Type/Php/ArrayChunkFunctionReturnTypeExtension.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
count: 2
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public function getValuesArray(): Type
return $this;
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this;
}

public function fillKeysArray(Type $valueType): Type
{
return new MixedType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public function unsetOffset(Type $offsetType): Type
return $this;
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return new NonEmptyArrayType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this once and the one in HasOffsetValueType is wrong. You should test what happens when you pass string&hasOffset to array_chunk.

A solution might be to return $this, or maybe handle this in IntersectionType::chunkArray (which knows what this is intersected with).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. It's not wrong IMO, but it could be better. I pushed tests to show the current behaviour.

Within HasOffset*Type we cannot return $this, that would be wrong because the offset ends up being nested after array_chunk. NonEmptyArray() is correct IMO but we could do better. See also https://3v4l.org/dCOoR which is the same as the type in the new tests (non-empty-list<non-empty-list<mixed>> or non-empty-list<non-empty-array>.

Using my new tests to check the current behaviour also shows that this PR would make it less precise, so that's definitely not good :/ https://phpstan.org/r/c4dfc504-4d6f-41fc-bad2-7bb9c90272bc

And yes, you're correct, looks like making it better means adapting IntersectionType most likely :) I'll try to push the fix soon.

Copy link
Contributor Author

@herndlm herndlm Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or is there a way to return this nested structure in the accessory type? so far I'm then getting a *NEVER* as result if I do that :/ guess I need to debug the types that it operates on a bit more..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind, I think this is easier than I thought 😊

Copy link
Contributor Author

@herndlm herndlm Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the noise :P

I managed to return whatever type I want, but realized that the current "live" behavior might be wrong already. e.g. how would we type the return type of https://3v4l.org/TfHjW assuming it's not a constant array but there are those 3 known offsets and there could be more we don't know about since its a generic array.

At first I thought it would be non-empty-list<hasOffsetValue('a','foo')&hasOffsetValue('b','bar')&hasOffsetValue('c','baz')&non-empty-array> but that's not correct since we have 3 arrays inside and the generic representation is just non-empty-list<non-empty-array>, right?

Then I thought we could use the offset and value info to make something like
non-empty-list<non-empty-array<'a'|'b'|'c','foo'|'bar'|'baz'>> but that's also wrong since, again, there could be other keys and values.

I seem to be coming back to the current behavior / result or am I missing something? As long as it's not a constant array we don't know in which "chunk" that offset (+ value) might end up in, right?

}

public function fillKeysArray(Type $valueType): Type
{
return new NonEmptyArrayType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public function getValuesArray(): Type
return new NonEmptyArrayType();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return new NonEmptyArrayType();
}

public function fillKeysArray(Type $valueType): Type
{
return new NonEmptyArrayType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public function getValuesArray(): Type
return $this;
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this;
}

public function fillKeysArray(Type $valueType): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public function getValuesArray(): Type
return $this;
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this;
}

public function fillKeysArray(Type $valueType): Type
{
return $this;
Expand Down
14 changes: 14 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,20 @@ public function unsetOffset(Type $offsetType): Type
return $this;
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
$chunkType = $preserveKeys->yes()
? $this
: AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $this->getIterableValueType()));
$chunkType = TypeCombinator::intersect($chunkType, new NonEmptyArrayType());

$arrayType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $chunkType));

return $this->isIterableAtLeastOnce()->yes()
? TypeCombinator::intersect($arrayType, new NonEmptyArrayType())
: $arrayType;
}

public function fillKeysArray(Type $valueType): Type
{
$itemType = $this->getItemType();
Expand Down
36 changes: 35 additions & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ConstantArrayType extends ArrayType implements ConstantType
{

private const DESCRIBE_LIMIT = 8;
private const CHUNK_FINITE_TYPES_LIMIT = 5;

private TrinaryLogic $isList;

Expand Down Expand Up @@ -780,6 +781,36 @@ public function unsetOffset(Type $offsetType): Type
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, $optionalKeys, $isList);
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
$biggerOne = IntegerRangeType::fromInterval(1, null);
$finiteTypes = $lengthType->getFiniteTypes();
if ($biggerOne->isSuperTypeOf($lengthType)->yes() && count($finiteTypes) < self::CHUNK_FINITE_TYPES_LIMIT) {
$results = [];
foreach ($finiteTypes as $finiteType) {
if (!$finiteType instanceof ConstantIntegerType || $finiteType->getValue() < 1) {
return parent::chunkArray($lengthType, $preserveKeys);
}

$length = $finiteType->getValue();

$builder = ConstantArrayTypeBuilder::createEmpty();

$keyTypesCount = count($this->keyTypes);
for ($i = 0; $i < $keyTypesCount; $i += $length) {
$chunk = $this->slice($i, $length, true);
$builder->setOffsetValueType(null, $preserveKeys->yes() ? $chunk : $chunk->getValuesArray());
}

$results[] = $builder->getArray();
}

return TypeCombinator::union(...$results);
}

return parent::chunkArray($lengthType, $preserveKeys);
}

public function fillKeysArray(Type $valueType): Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
Expand Down Expand Up @@ -1185,7 +1216,10 @@ public function reverse(bool $preserveKeys = false): self
return $this->reverseConstantArray(TrinaryLogic::createFromBoolean($preserveKeys));
}

/** @param positive-int $length */
/**
* @deprecated Use chunkArray() instead
* @param positive-int $length
*/
public function chunk(int $length, bool $preserveKeys = false): self
{
$builder = ConstantArrayTypeBuilder::createEmpty();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ public function getValuesArray(): Type
return $this->intersectTypes(static fn (Type $type): Type => $type->getValuesArray());
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->chunkArray($lengthType, $preserveKeys));
}

public function fillKeysArray(Type $valueType): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->fillKeysArray($valueType));
Expand Down
9 changes: 9 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public function getValuesArray(): Type
return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new MixedType($this->isExplicitMixed)));
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
if ($this->isArray()->no()) {
return new ErrorType();
}

return AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new MixedType($this->isExplicitMixed)));
}

public function fillKeysArray(Type $valueType): Type
{
if ($this->isArray()->no()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ public function getValuesArray(): Type
return new NeverType();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return new NeverType();
}

public function fillKeysArray(Type $valueType): Type
{
return new NeverType();
Expand Down
68 changes: 6 additions & 62 deletions src/Type/Php/ArrayChunkFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

final class ArrayChunkFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

private const FINITE_TYPES_LIMIT = 5;

public function __construct(private PhpVersion $phpVersion)
{
}
Expand All @@ -41,68 +33,20 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$lengthType = $scope->getType($functionCall->getArgs()[1]->value);
if (isset($functionCall->getArgs()[2])) {
$preserveKeysType = $scope->getType($functionCall->getArgs()[2]->value);
$preserveKeys = $preserveKeysType instanceof ConstantBooleanType ? $preserveKeysType->getValue() : null;
} else {
$preserveKeys = false;
if ($arrayType->isArray()->no()) {
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
}

$lengthType = $scope->getType($functionCall->getArgs()[1]->value);
$negativeOrZero = IntegerRangeType::fromInterval(null, 0);
if ($negativeOrZero->isSuperTypeOf($lengthType)->yes()) {
return $this->phpVersion->throwsValueErrorForInternalFunctions() ? new NeverType() : new NullType();
}

if (!$arrayType->isArray()->yes()) {
return null;
}

if ($preserveKeys !== null) {
$constantArrays = $arrayType->getConstantArrays();
$biggerOne = IntegerRangeType::fromInterval(1, null);
$finiteTypes = $lengthType->getFiniteTypes();
if (count($constantArrays) > 0
&& $biggerOne->isSuperTypeOf($lengthType)->yes()
&& count($finiteTypes) < self::FINITE_TYPES_LIMIT
) {
$results = [];
foreach ($constantArrays as $constantArray) {
foreach ($finiteTypes as $finiteType) {
if (!$finiteType instanceof ConstantIntegerType || $finiteType->getValue() < 1) {
return null;
}

$results[] = $constantArray->chunk($finiteType->getValue(), $preserveKeys);
}
}

return TypeCombinator::union(...$results);
}
}

$chunkType = self::getChunkType($arrayType, $preserveKeys);

$resultType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $chunkType));
if ($arrayType->isIterableAtLeastOnce()->yes()) {
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
}

return $resultType;
}

private static function getChunkType(Type $type, ?bool $preserveKeys): Type
{
if ($preserveKeys === null) {
$chunkType = new ArrayType(TypeCombinator::union($type->getIterableKeyType(), new IntegerType()), $type->getIterableValueType());
} elseif ($preserveKeys) {
$chunkType = $type;
} else {
$chunkType = new ArrayType(new IntegerType(), $type->getIterableValueType());
$chunkType = AccessoryArrayListType::intersectWith($chunkType);
}
$preserveKeysType = isset($functionCall->getArgs()[2]) ? $scope->getType($functionCall->getArgs()[2]->value) : new ConstantBooleanType(false);
$preserveKeys = (new ConstantBooleanType(true))->isSuperTypeOf($preserveKeysType);

return TypeCombinator::intersect($chunkType, new NonEmptyArrayType());
return $arrayType->chunkArray($lengthType, $preserveKeys);
}

}
5 changes: 5 additions & 0 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ public function getValuesArray(): Type
return $this->getStaticObjectType()->getValuesArray();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this->getStaticObjectType()->chunkArray($lengthType, $preserveKeys);
}

public function fillKeysArray(Type $valueType): Type
{
return $this->getStaticObjectType()->fillKeysArray($valueType);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public function getValuesArray(): Type
return $this->resolve()->getValuesArray();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this->resolve()->chunkArray($lengthType, $preserveKeys);
}

public function fillKeysArray(Type $valueType): Type
{
return $this->resolve()->fillKeysArray($valueType);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/MaybeArrayTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function getValuesArray(): Type
return new ErrorType();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return new ErrorType();
}

public function fillKeysArray(Type $valueType): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/NonArrayTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function getValuesArray(): Type
return new ErrorType();
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return new ErrorType();
}

public function fillKeysArray(Type $valueType): Type
{
return new ErrorType();
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function getKeysArray(): Type;

public function getValuesArray(): Type;

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type;

public function fillKeysArray(Type $valueType): Type;

public function flipArray(): Type;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ public function getValuesArray(): Type
return $this->unionTypes(static fn (Type $type): Type => $type->getValuesArray());
}

public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
{
return $this->unionTypes(static fn (Type $type): Type => $type->chunkArray($lengthType, $preserveKeys));
}

public function fillKeysArray(Type $valueType): Type
{
return $this->unionTypes(static fn (Type $type): Type => $type->fillKeysArray($valueType));
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/array-chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,26 @@ function testLimits(array $arr, int $oneToFour, int $tooBig) {
assertType('non-empty-list<non-empty-list<0|1|2|3>>', array_chunk($arr, $tooBig));
}

/** @param array<string, string> $map */
public function offsets(array $arr, array $map): void
{
if (array_key_exists('foo', $arr)) {
assertType('non-empty-list<non-empty-list<mixed>>', array_chunk($arr, 2));
assertType('non-empty-list<non-empty-array>', array_chunk($arr, 2, true));
}
if (array_key_exists('foo', $arr) && $arr['foo'] === 'bar') {
assertType('non-empty-list<non-empty-list<mixed>>', array_chunk($arr, 2));
assertType('non-empty-list<non-empty-array>', array_chunk($arr, 2, true));
}

if (array_key_exists('foo', $map)) {
assertType('non-empty-list<non-empty-list<string>>', array_chunk($map, 2));
assertType('non-empty-list<non-empty-array<string, string>>', array_chunk($map, 2, true));
}
if (array_key_exists('foo', $map) && $map['foo'] === 'bar') {
assertType('non-empty-list<non-empty-list<string>>', array_chunk($map, 2));
assertType('non-empty-list<non-empty-array<string, string>>', array_chunk($map, 2, true));
}
}

}
Loading