Skip to content
Open
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
9 changes: 5 additions & 4 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,9 @@ public function fillKeysArray(Type $valueType): Type
return $stringKeyType;
}

$builder->setOffsetValueType($stringKeyType, $valueType, $this->isOptionalKey($i));
$builder->setOffsetValueType($stringKeyType, $valueType, $this->isOptionalKey($i) || count($stringKeyType->getConstantScalarTypes()) > 1);
} else {
$builder->setOffsetValueType($keyType, $valueType, $this->isOptionalKey($i));
$builder->setOffsetValueType($keyType, $valueType, $this->isOptionalKey($i) || count($keyType->getConstantScalarTypes()) > 1);
}
}

Expand All @@ -984,10 +984,11 @@ public function flipArray(): Type

foreach ($this->keyTypes as $i => $keyType) {
$valueType = $this->valueTypes[$i];
$offsetType = $valueType->toArrayKey();
$builder->setOffsetValueType(
$valueType->toArrayKey(),
$offsetType,
$keyType,
$this->isOptionalKey($i),
$this->isOptionalKey($i) || count($offsetType->getConstantScalarTypes()) > 1,
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/Type/Php/ArrayFillKeysFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

#[AutowiredService]
Expand Down Expand Up @@ -38,7 +40,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
}

return $keysType->fillKeysArray($scope->getType($args[1]->value));
$filled = $keysType->fillKeysArray($scope->getType($args[1]->value));
if ($keysType->isIterableAtLeastOnce()->yes() && $filled->isArray()->yes()) {
return TypeCombinator::intersect($filled, new NonEmptyArrayType());
}
return $filled;
}

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

namespace Bug14656;

use function PHPStan\Testing\assertType;

class ArrayFlipUnionValues
{
/** @param array{0: 'a'|'b'|'c', 1: 'a'|'b'|'c', 2: 'a'|'b'|'c'} $a */
public function allUnion(array $a): void
{
assertType("non-empty-array{a?: 0|1|2, b?: 0|1|2, c?: 0|1|2}", array_flip($a));
}

/** @param array{0: 'a'|'b', 1: 'b'|'c'} $a */
public function overlappingUnion(array $a): void
{
assertType("non-empty-array<'a'|'b'|'c', 0|1>", array_flip($a));
}

/** @param array{0: 'a'|'b', 1: 'c'} $a */
public function mixedUnionAndConstant(array $a): void
{
assertType("array{a?: 0, b?: 0, c: 1}", array_flip($a));
}
}

class ArrayFillKeysUnionValues
{
/** @param array{0: 'a'|'b', 1: 'b'|'c'} $a */
public function overlappingUnion(array $a): void
{
assertType("non-empty-array<'a'|'b'|'c', 'x'>", array_fill_keys($a, 'x'));
}

/** @param array{0: 'a'|'b'|'c', 1: 'a'|'b'|'c', 2: 'a'|'b'|'c'} $a */
public function allUnion(array $a): void
{
assertType("non-empty-array{a?: 'x', b?: 'x', c?: 'x'}", array_fill_keys($a, 'x'));
}

/** @param array{0: 'a'|'b', 1: 'c'} $a */
public function mixedUnionAndConstant(array $a): void
{
assertType("array{a?: 'x', b?: 'x', c: 'x'}", array_fill_keys($a, 'x'));
}
}
Loading