Skip to content

Commit 8084ab3

Browse files
herndlmondrejmirtes
authored andcommitted
Fix array_merge with numeric keys
1 parent c7ed571 commit 8084ab3

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\ArrayType;
1212
use PHPStan\Type\Constant\ConstantArrayType;
1313
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
14+
use PHPStan\Type\Constant\ConstantIntegerType;
1415
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1516
use PHPStan\Type\NeverType;
1617
use PHPStan\Type\Type;
@@ -64,7 +65,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6465
$isOptional = in_array($k, $optionalKeys, true);
6566

6667
$newArrayBuilder->setOffsetValueType(
67-
$keyType,
68+
$keyType instanceof ConstantIntegerType ? null : $keyType,
6869
$valueTypes[$k],
6970
$isOptional,
7071
);

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4727,7 +4727,7 @@ public function dataArrayFunctions(): array
47274727
'array_values($generalStringKeys)',
47284728
],
47294729
[
4730-
'array{foo: stdClass, 1: stdClass}',
4730+
'array{foo: stdClass, 0: stdClass}',
47314731
'array_merge($stringOrIntegerKeys)',
47324732
],
47334733
[
@@ -4743,15 +4743,15 @@ public function dataArrayFunctions(): array
47434743
'array_merge($stringOrIntegerKeys, $generalStringKeys)',
47444744
],
47454745
[
4746-
'array{foo: stdClass, bar: stdClass, 1: stdClass}',
4746+
'array{foo: stdClass, bar: stdClass, 0: stdClass}',
47474747
'array_merge($stringKeys, $stringOrIntegerKeys)',
47484748
],
47494749
[
4750-
"array{foo: 'foo', 1: stdClass, bar: stdClass}",
4750+
"array{foo: 'foo', 0: stdClass, bar: stdClass}",
47514751
'array_merge($stringOrIntegerKeys, $stringKeys)',
47524752
],
47534753
[
4754-
"array{color: 'green', 0: 'a', 1: 'b', shape: 'trapezoid', 2: 4}",
4754+
"array{color: 'green', 0: 2, 1: 4, 2: 'a', 3: 'b', shape: 'trapezoid', 4: 4}",
47554755
'array_merge(array("color" => "red", 2, 4), array("a", "b", "color" => "green", "shape" => "trapezoid", 4))',
47564756
],
47574757
[

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ public function dataFileAsserts(): iterable
309309
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-flip.php');
310310
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map.php');
311311
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map-closure.php');
312+
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-merge.php');
312313
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-sum.php');
313314
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-plus.php');
314315
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4573.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ArrayMerge;
4+
5+
use function array_merge;
6+
use function PHPStan\Testing\assertType;
7+
8+
function foo(): void
9+
{
10+
$foo = ['foo' => 17, 'a', 'bar' => 18, 'b'];
11+
$bar = [99 => 'b', 'bar' => 19, 98 => 'c'];
12+
$baz = array_merge($foo, $bar);
13+
14+
assertType('array{foo: 17, 0: \'a\', bar: 19, 1: \'b\', 2: \'b\', 3: \'c\'}', $baz);
15+
}

0 commit comments

Comments
 (0)