Skip to content

Commit ad54a20

Browse files
committed
Simplify some more
1 parent 573ef62 commit ad54a20

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,37 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4040

4141
$argTypes = [];
4242
$optionalArgTypes = [];
43-
$allConstant = TrinaryLogic::createYes();
4443
foreach ($args as $arg) {
4544
$argType = $scope->getType($arg->value);
4645

4746
if ($arg->unpack) {
4847
if ($argType->isConstantArray()->yes()) {
49-
$argTypesFound = [];
5048
foreach ($argType->getConstantArrays() as $constantArray) {
5149
foreach ($constantArray->getValueTypes() as $valueType) {
52-
$argTypesFound[] = $valueType;
50+
$argTypes[] = $valueType;
5351
}
5452
}
5553
} else {
56-
$argTypesFound = [$argType->getIterableValueType()];
57-
}
58-
59-
foreach ($argTypesFound as $argTypeFound) {
60-
$argTypes[] = $argTypeFound;
61-
$allConstant = $allConstant->and($argTypeFound->isConstantArray());
54+
$argTypes[] = $argType->getIterableValueType();
6255
}
6356

6457
if (!$argType->isIterableAtLeastOnce()->yes()) {
6558
// unpacked params can be empty, making them optional
6659
$optionalArgTypesOffset = count($argTypes) - 1;
67-
foreach (array_keys($argTypesFound) as $key) {
60+
foreach (array_keys($argTypes) as $key) {
6861
$optionalArgTypes[] = $optionalArgTypesOffset + $key;
6962
}
7063
}
7164
} else {
7265
$argTypes[] = $argType;
73-
$allConstant = $allConstant->and($argType->isConstantArray());
7466
}
7567
}
7668

69+
$allConstant = TrinaryLogic::createYes()->lazyAnd(
70+
$argTypes,
71+
static fn (Type $argType) => $argType->isConstantArray(),
72+
);
73+
7774
if ($allConstant->yes()) {
7875
$newArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
7976
foreach ($argTypes as $argType) {

tests/PHPStan/Analyser/nsrt/array-merge2.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function arrayMergeArrayShapes($array1, $array2): void
2323
assertType("array{foo: 3, bar: '2', lall2: '3', 0: '4', 1: '6', lall: '3', 2: '2', 3: '3'}", array_merge($array2, $array1, ...[['foo' => 3]]));
2424
assertType("array{foo: '1', bar: '2'|'4', lall?: '3', 0: '2'|'4', 1: '3'|'6', lall2?: '3'}", array_merge(rand(0, 1) ? $array1 : $array2, []));
2525
assertType("array{foo?: 3, bar?: 3}", array_merge([], ...[rand(0, 1) ? ['foo' => 3] : ['bar' => 3]]));
26+
assertType("array{foo: '1', bar: '2'|'4', lall?: '3', 0: '2'|'4', 1: '3'|'6', lall2?: '3'}", array_merge([], ...[rand(0, 1) ? $array1 : $array2]));
27+
assertType("array{foo: 1, bar: 2, 0: 2, 1: 3}", array_merge(['foo' => 4, 'bar' => 5], ...[['foo' => 1, 'bar' => 2], [2, 3]]));
2628
}
2729

2830
/**

0 commit comments

Comments
 (0)