Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array_merge with numeric keys #1218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$isOptional = in_array($k, $optionalKeys, true);

$newArrayBuilder->setOffsetValueType(
$keyType,
$keyType instanceof ConstantIntegerType ? null : $keyType,
$valueTypes[$k],
$isOptional,
);
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Expand Up @@ -4727,7 +4727,7 @@ public function dataArrayFunctions(): array
'array_values($generalStringKeys)',
],
[
'array{foo: stdClass, 1: stdClass}',
'array{foo: stdClass, 0: stdClass}',
'array_merge($stringOrIntegerKeys)',
],
[
Expand All @@ -4743,15 +4743,15 @@ public function dataArrayFunctions(): array
'array_merge($stringOrIntegerKeys, $generalStringKeys)',
],
[
'array{foo: stdClass, bar: stdClass, 1: stdClass}',
'array{foo: stdClass, bar: stdClass, 0: stdClass}',
'array_merge($stringKeys, $stringOrIntegerKeys)',
],
[
"array{foo: 'foo', 1: stdClass, bar: stdClass}",
"array{foo: 'foo', 0: stdClass, bar: stdClass}",
'array_merge($stringOrIntegerKeys, $stringKeys)',
],
[
"array{color: 'green', 0: 'a', 1: 'b', shape: 'trapezoid', 2: 4}",
"array{color: 'green', 0: 2, 1: 4, 2: 'a', 3: 'b', shape: 'trapezoid', 4: 4}",
'array_merge(array("color" => "red", 2, 4), array("a", "b", "color" => "green", "shape" => "trapezoid", 4))',
],
[
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -309,6 +309,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-flip.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map-closure.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-merge.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-sum.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-plus.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4573.php');
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/array-merge.php
@@ -0,0 +1,15 @@
<?php

namespace ArrayMerge;

use function array_merge;
use function PHPStan\Testing\assertType;

function foo(): void
{
$foo = ['foo' => 17, 'a', 'bar' => 18, 'b'];
$bar = [99 => 'b', 'bar' => 19, 98 => 'c'];
$baz = array_merge($foo, $bar);

assertType('array{foo: 17, 0: \'a\', bar: 19, 1: \'b\', 2: \'b\', 3: \'c\'}', $baz);
}