Skip to content
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
16 changes: 14 additions & 2 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ final class ConstantArrayTypeBuilder

private bool $oversized = false;

private TrinaryLogic $isNonEmpty;

/**
* @param list<Type> $keyTypes
* @param array<int, Type> $valueTypes
Expand All @@ -55,6 +57,7 @@ private function __construct(
private TrinaryLogic $isList,
)
{
$this->isNonEmpty = TrinaryLogic::createNo();
}

public static function createEmpty(): self
Expand All @@ -71,6 +74,7 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
$startArrayType->getOptionalKeys(),
$startArrayType->isList(),
);
$builder->isNonEmpty = $startArrayType->isIterableAtLeastOnce();

if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
$builder->degradeToGeneralArray(true);
Expand All @@ -89,6 +93,10 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
return;
}

if (!$optional) {
$this->isNonEmpty = TrinaryLogic::createYes();
}

if (!$this->degradeToGeneralArray) {
if (
$valueType instanceof ClosureType
Expand Down Expand Up @@ -388,7 +396,11 @@ public function getArray(): Type
if (!$this->degradeToGeneralArray) {
/** @var list<ConstantIntegerType|ConstantStringType> $keyTypes */
$keyTypes = $this->keyTypes;
return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
$array = new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
if ($this->isNonEmpty->yes() && !$array->isIterableAtLeastOnce()->yes()) {
return TypeCombinator::intersect($array, new NonEmptyArrayType());
}
return $array;
}

if ($this->degradeClosures === true) {
Expand All @@ -410,7 +422,7 @@ public function getArray(): Type
);

$types = [];
if (count($this->optionalKeys) < $keyTypesCount) {
if ($this->isNonEmpty->yes() || count($this->optionalKeys) < $keyTypesCount) {
$types[] = new NonEmptyArrayType();
}

Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/bug-14551.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function foreachTwoKeys(array $keys): void
$result[$k]['y'] = 2;
}

assertType("array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}}", $result);
assertType("non-empty-array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}}", $result);
}

/**
Expand All @@ -30,7 +30,7 @@ function foreachThreeKeys(array $keys): void
$result[$k]['z'] = 3;
}

assertType("array{a?: array{x: 1, y: 2, z: 3}, b?: array{x: 1, y: 2, z: 3}}", $result);
assertType("non-empty-array{a?: array{x: 1, y: 2, z: 3}, b?: array{x: 1, y: 2, z: 3}}", $result);
}

/**
Expand All @@ -43,7 +43,7 @@ function withoutForeach(string $k): void
$result[$k]['x'] = 1;
$result[$k]['y'] = 2;

assertType("array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}}", $result);
assertType("non-empty-array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}}", $result);
}

/**
Expand All @@ -56,7 +56,7 @@ function integerKeys(int $k): void
$result[$k]['x'] = 1;
$result[$k]['y'] = 2;

assertType("array{0?: array{x: 1, y: 2}, 1?: array{x: 1, y: 2}}", $result);
assertType("non-empty-array{0?: array{x: 1, y: 2}, 1?: array{x: 1, y: 2}}", $result);
}

/**
Expand All @@ -69,7 +69,7 @@ function threeWayUnion(string $k): void
$result[$k]['x'] = 1;
$result[$k]['y'] = 2;

assertType("array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}, c?: array{x: 1, y: 2}}", $result);
assertType("non-empty-array{a?: array{x: 1, y: 2}, b?: array{x: 1, y: 2}, c?: array{x: 1, y: 2}}", $result);
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14552.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types = 1);

namespace Bug14552;

use function PHPStan\Testing\assertType;

/**
* @param non-empty-list<'a'|'b'> $keys
*/
function nonEmptyListForeach(array $keys): void
{
$out = [];
foreach ($keys as $k) {
$out[$k] = 1;
}
assertType("non-empty-array{a?: 1, b?: 1}", $out);
}

/**
* @param list<'a'|'b'> $keys
*/
function possiblyEmptyListForeach(array $keys): void
{
$out = [];
foreach ($keys as $k) {
$out[$k] = 1;
}
assertType("array{}|array{a?: 1, b?: 1}", $out);
}

/**
* @param non-empty-list<'x'|'y'|'z'> $keys
*/
function nonEmptyListThreeKeys(array $keys): void
{
$out = [];
foreach ($keys as $k) {
$out[$k] = true;
}
assertType("non-empty-array{x?: true, y?: true, z?: true}", $out);
}

/**
* Direct assignment (non-foreach) with union key on empty array.
* @param 'a'|'b' $key
*/
function directAssignment(string $key): void
{
$arr = [];
$arr[$key] = 1;
assertType("non-empty-array{a?: 1, b?: 1}", $arr);
}

/**
* Direct assignment with integer union key on empty array.
* @param 0|1|2 $key
*/
function directAssignmentIntKey(int $key): void
{
$arr = [];
$arr[$key] = 'val';
assertType("non-empty-array{0?: 'val', 1?: 'val', 2?: 'val'}", $arr);
}

/**
* Setting union key on already non-empty array should stay non-empty.
* @param 'x'|'y' $key
*/
function setOnNonEmptyArray(string $key): void
{
$arr = ['existing' => 0];
$arr[$key] = 1;
assertType("array{existing: 0, x?: 1, y?: 1}", $arr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function doFoo(): void

$k = rand(0, 1) ? 1 : 2;
$a[$k] = true;
assertType('array{1?: true, 2?: true}', $a);
assertType('non-empty-array{1?: true, 2?: true}', $a);
}

}
46 changes: 46 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,50 @@ public function testSetOffsetValueTypeOnConstantArrayWithEmptyNextAutoIndexesRet
$this->assertInstanceOf(ErrorType::class, $result);
}

public function testNonOptionalUnionOffsetOnEmptyArrayIsNonEmpty(): void
{
$builder = ConstantArrayTypeBuilder::createEmpty();

$aOrB = TypeCombinator::union(
new ConstantStringType('a'),
new ConstantStringType('b'),
);
$builder->setOffsetValueType($aOrB, new ConstantIntegerType(1));

$array = $builder->getArray();
$this->assertSame('non-empty-array{a?: 1, b?: 1}', $array->describe(VerbosityLevel::precise()));
}

public function testOptionalSingleOffsetOnEmptyArrayIsPossiblyEmpty(): void
{
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(new ConstantStringType('a'), new ConstantIntegerType(1), true);

$array = $builder->getArray();
$this->assertSame('array{a?: 1}', $array->describe(VerbosityLevel::precise()));
}

public function testOptionalUnionOffsetOnEmptyArrayIsPossiblyEmpty(): void
{
$builder = ConstantArrayTypeBuilder::createEmpty();

$aOrB = TypeCombinator::union(
new ConstantStringType('a'),
new ConstantStringType('b'),
);
$builder->setOffsetValueType($aOrB, new ConstantIntegerType(1), true);

$array = $builder->getArray();
$this->assertSame('array{a?: 1, b?: 1}', $array->describe(VerbosityLevel::precise()));
}

public function testOptionalNullOffsetOnEmptyArrayIsPossiblyEmpty(): void
{
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType(null, new ConstantIntegerType(1), true);

$array = $builder->getArray();
$this->assertSame('array{0?: 1}', $array->describe(VerbosityLevel::precise()));
}

}
Loading