Skip to content

Commit e2fa790

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.0.x
2 parents 7b4c9af + 4f142f5 commit e2fa790

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/Type/Php/IteratorToArrayFunctionReturnTypeExtension.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,28 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3232
}
3333

3434
$traversableType = $scope->getType($arguments[0]->value);
35-
$arrayKeyType = $traversableType->getIterableKeyType()->toArrayKey();
36-
37-
if ($arrayKeyType instanceof ErrorType) {
38-
return new NeverType(true);
39-
}
40-
41-
$isList = false;
4235

4336
if (isset($arguments[1])) {
4437
$preserveKeysType = $scope->getType($arguments[1]->value);
4538

4639
if ($preserveKeysType->isFalse()->yes()) {
47-
$arrayKeyType = new IntegerType();
48-
$isList = true;
40+
return TypeCombinator::intersect(new ArrayType(
41+
new IntegerType(),
42+
$traversableType->getIterableValueType(),
43+
), new AccessoryArrayListType());
4944
}
5045
}
5146

52-
$arrayType = new ArrayType(
53-
$arrayKeyType,
54-
$traversableType->getIterableValueType(),
55-
);
47+
$arrayKeyType = $traversableType->getIterableKeyType()->toArrayKey();
5648

57-
if ($isList) {
58-
$arrayType = TypeCombinator::intersect($arrayType, new AccessoryArrayListType());
49+
if ($arrayKeyType instanceof ErrorType) {
50+
return new NeverType(true);
5951
}
6052

61-
return $arrayType;
53+
return new ArrayType(
54+
$arrayKeyType,
55+
$traversableType->getIterableValueType(),
56+
);
6257
}
6358

6459
}

tests/PHPStan/Analyser/nsrt/iterator_to_array.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function testBehaviorOnGenerators(): void
4949
};
5050

5151
assertType('array<0|1|2|\'\', 1|2|3|4>', iterator_to_array($generator1()));
52+
assertType('list<1|2|3|4>', iterator_to_array($generator1(), false));
5253
assertType('array<0|1|\'\'|\'a\', 1|2|3|4>', iterator_to_array($generator2()));
54+
assertType('list<1|2|3|4>', iterator_to_array($generator2(), false));
5355
}
5456

5557
public function testOnGeneratorsWithIllegalKeysForArray(): void
@@ -60,5 +62,6 @@ public function testOnGeneratorsWithIllegalKeysForArray(): void
6062
};
6163

6264
assertType('*NEVER*', iterator_to_array($illegalGenerator()));
65+
assertType('list<\'b\'|\'c\'>', iterator_to_array($illegalGenerator(), false));
6366
}
6467
}

0 commit comments

Comments
 (0)