Skip to content

Commit 53817aa

Browse files
committed
Revert support array-shapes in array_merge
This reverts commit f21b218.
1 parent d28f785 commit 53817aa

File tree

4 files changed

+17
-138
lines changed

4 files changed

+17
-138
lines changed

src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use PHPStan\Reflection\ParametersAcceptorSelector;
99
use PHPStan\Type\Accessory\NonEmptyArrayType;
1010
use PHPStan\Type\ArrayType;
11-
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1211
use PHPStan\Type\GeneralizePrecision;
1312
use PHPStan\Type\Type;
1413
use PHPStan\Type\TypeCombinator;
1514
use PHPStan\Type\TypeUtils;
15+
use PHPStan\Type\UnionType;
1616

1717
class ArrayMergeFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
1818
{
@@ -30,63 +30,32 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3030

3131
$keyTypes = [];
3232
$valueTypes = [];
33-
$returnedArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
34-
$returnedArrayBuilderFilled = false;
3533
$nonEmpty = false;
36-
$offsetCount = 0;
3734
foreach ($functionCall->args as $arg) {
3835
$argType = $scope->getType($arg->value);
39-
4036
if ($arg->unpack) {
4137
$argType = $argType->getIterableValueType();
42-
}
43-
44-
$arrays = TypeUtils::getConstantArrays($argType);
45-
if (count($arrays) > 0) {
46-
foreach ($arrays as $constantArray) {
47-
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
48-
$returnedArrayBuilderFilled = true;
49-
$offsetCount++;
50-
if ($offsetCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
51-
$returnedArrayBuilder->degradeToGeneralArray();
52-
}
53-
54-
$returnedArrayBuilder->setOffsetValueType(
55-
is_numeric($keyType->getValue()) ? null : $keyType,
56-
$constantArray->getValueTypes()[$i]
57-
);
38+
if ($argType instanceof UnionType) {
39+
foreach ($argType->getTypes() as $innerType) {
40+
$argType = $innerType;
5841
}
5942
}
60-
61-
} else {
62-
$keyTypes[] = TypeUtils::generalizeType($argType->getIterableKeyType(), GeneralizePrecision::moreSpecific());
63-
$valueTypes[] = $argType->getIterableValueType();
6443
}
6544

45+
$keyTypes[] = TypeUtils::generalizeType($argType->getIterableKeyType(), GeneralizePrecision::moreSpecific());
46+
$valueTypes[] = $argType->getIterableValueType();
47+
6648
if (!$argType->isIterableAtLeastOnce()->yes()) {
6749
continue;
6850
}
6951

7052
$nonEmpty = true;
7153
}
7254

73-
if (count($keyTypes) > 0) {
74-
$arrayType = new ArrayType(
75-
TypeCombinator::union(...$keyTypes),
76-
TypeCombinator::union(...$valueTypes)
77-
);
78-
79-
if ($returnedArrayBuilderFilled) {
80-
$arrayType = TypeCombinator::union($returnedArrayBuilder->getArray(), $arrayType);
81-
}
82-
} elseif ($returnedArrayBuilderFilled) {
83-
$arrayType = $returnedArrayBuilder->getArray();
84-
} else {
85-
$arrayType = new ArrayType(
86-
TypeCombinator::union(...$keyTypes),
87-
TypeCombinator::union(...$valueTypes)
88-
);
89-
}
55+
$arrayType = new ArrayType(
56+
TypeCombinator::union(...$keyTypes),
57+
TypeCombinator::union(...$valueTypes)
58+
);
9059

9160
if ($nonEmpty) {
9261
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5153,44 +5153,31 @@ public function dataArrayFunctions(): array
51535153
'array_values($generalStringKeys)',
51545154
],
51555155
[
5156-
"array('foo' => stdClass, 0 => stdClass)",
5156+
'array<int|non-empty-string, stdClass>&nonEmpty',
51575157
'array_merge($stringOrIntegerKeys)',
51585158
],
51595159
[
51605160
'array<int|string, DateTimeImmutable|int>',
51615161
'array_merge($generalStringKeys, $generalDateTimeValues)',
51625162
],
51635163
[
5164-
'array<0|string, int|stdClass>&nonEmpty',
5164+
'array<int|string, int|stdClass>&nonEmpty',
51655165
'array_merge($generalStringKeys, $stringOrIntegerKeys)',
51665166
],
51675167
[
5168-
'array<0|string, int|stdClass>&nonEmpty',
5168+
'array<int|string, int|stdClass>&nonEmpty',
51695169
'array_merge($stringOrIntegerKeys, $generalStringKeys)',
51705170
],
51715171
[
5172-
"array('foo' => stdClass, 'bar' => stdClass, 0 => stdClass)",
5172+
'array<int|non-empty-string, \'foo\'|stdClass>&nonEmpty',
51735173
'array_merge($stringKeys, $stringOrIntegerKeys)',
51745174
],
51755175
[
5176-
"array('foo' => 1, 'bar' => 2, 0 => 2, 1 => 3)",
5177-
"array_merge(['foo' => 4, 'bar' => 5], ...[['foo' => 1, 'bar' => 2], [2, 3]])",
5178-
],
5179-
[
5180-
"array('foo' => 1, 'foo2' => stdClass)",
5181-
'array_merge([\'foo\' => new stdClass()], ...[[\'foo2\' => new stdClass()], [\'foo\' => 1]])',
5182-
],
5183-
5184-
[
5185-
"array('foo' => 1, 'foo2' => stdClass)",
5186-
'array_merge([\'foo\' => new stdClass()], ...[[\'foo2\' => new stdClass()], [\'foo\' => 1]])',
5187-
],
5188-
[
5189-
"array('foo' => 'foo', 0 => stdClass, 'bar' => stdClass)",
5176+
'array<int|non-empty-string, \'foo\'|stdClass>&nonEmpty',
51905177
'array_merge($stringOrIntegerKeys, $stringKeys)',
51915178
],
51925179
[
5193-
"array('color' => 'green', 0 => 2, 1 => 4, 2 => 'a', 3 => 'b', 'shape' => 'trapezoid', 4 => 4)",
5180+
'array<int|non-empty-string, 2|4|\'a\'|\'b\'|\'green\'|\'red\'|\'trapezoid\'>&nonEmpty',
51945181
'array_merge(array("color" => "red", 2, 4), array("a", "b", "color" => "green", "shape" => "trapezoid", 4))',
51955182
],
51965183
[

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ public function dataFileAsserts(): iterable
134134
yield from $this->gatherAssertTypes(__DIR__ . '/data/throw-expr.php');
135135
}
136136

137-
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-merge.php');
138-
139137
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-array.php');
140138

141139
if (PHP_VERSION_ID >= 80000 || self::$useStaticReflectionProvider) {

tests/PHPStan/Analyser/data/array-merge.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)