Skip to content

Commit c9ef256

Browse files
committed
moved array_flip() improvement into ArrayFlipFunctionReturnTypeExtension
1 parent 6c99994 commit c9ef256

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Diff for: src/Type/IntersectionType.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,7 @@ public function fillKeysArray(Type $valueType): Type
861861

862862
public function flipArray(): Type
863863
{
864-
$flipped = $this->intersectTypes(static fn (Type $type): Type => $type->flipArray());
865-
if ($this->isIterableAtLeastOnce()->yes()) {
866-
return TypeCombinator::intersect($flipped, new NonEmptyArrayType());
867-
}
868-
return $flipped;
864+
return $this->intersectTypes(static fn (Type $type): Type => $type->flipArray());
869865
}
870866

871867
public function intersectKeyArray(Type $otherArraysType): Type

Diff for: src/Type/Php/ArrayFlipFunctionReturnTypeExtension.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Php\PhpVersion;
88
use PHPStan\Reflection\FunctionReflection;
9+
use PHPStan\Type\Accessory\NonEmptyArrayType;
910
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1011
use PHPStan\Type\NeverType;
1112
use PHPStan\Type\NullType;
1213
use PHPStan\Type\Type;
14+
use PHPStan\Type\TypeCombinator;
1315
use function count;
1416

1517
final class ArrayFlipFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -35,7 +37,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3537
return $this->phpVersion->arrayFunctionsReturnNullWithNonArray() ? new NullType() : new NeverType();
3638
}
3739

38-
return $arrayType->flipArray();
40+
$flipped = $arrayType->flipArray();
41+
if ($arrayType->isIterableAtLeastOnce()->yes()) {
42+
return TypeCombinator::intersect($flipped, new NonEmptyArrayType());
43+
}
44+
return $flipped;
3945
}
4046

4147
}

0 commit comments

Comments
 (0)