Skip to content

Commit

Permalink
Retain list type via array_push() and array_unshift()
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jun 18, 2023
1 parent 92deb3f commit 1f421d4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
return;
}

$arrayType = new ArrayType($arrayType->getIterableKeyType(), $arrayType->getIterableValueType());
$arrayType = TypeCombinator::union($arrayType, new ConstantArrayType([], []));
},
);

Expand Down
8 changes: 7 additions & 1 deletion tests/PHPStan/Analyser/data/array-push.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* @param int[] $b
* @param non-empty-array<int> $c
* @param array<int|string> $d
* @param list<string> $e
*/
function arrayPush(array $a, array $b, array $c, array $d, array $arr): void
function arrayPush(array $a, array $b, array $c, array $d, array $e, array $arr): void
{
array_push($a, ...$b);
assertType('array<int|string>', $a);
Expand All @@ -32,6 +33,11 @@ function arrayPush(array $a, array $b, array $c, array $d, array $arr): void
$d1 = [];
array_push($d, ...$d1);
assertType('array<bool|int|string|null>', $d);

/** @var list<bool> $e1 */
$e1 = [];
array_push($e, ...$e1);
assertType('list<bool|string>', $e);
}

function arrayPushConstantArray(): void
Expand Down
8 changes: 7 additions & 1 deletion tests/PHPStan/Analyser/data/array-unshift.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* @param int[] $b
* @param non-empty-array<int> $c
* @param array<int|string> $d
* @param list<string> $e
*/
function arrayUnshift(array $a, array $b, array $c, array $d, array $arr): void
function arrayUnshift(array $a, array $b, array $c, array $d, array $e, array $arr): void
{
array_unshift($a, ...$b);
assertType('array<int|string>', $a);
Expand All @@ -32,6 +33,11 @@ function arrayUnshift(array $a, array $b, array $c, array $d, array $arr): void
$d1 = [];
array_unshift($d, ...$d1);
assertType('array<bool|int|string|null>', $d);

/** @var list<bool> $e1 */
$e1 = [];
array_unshift($e, ...$e1);
assertType('list<bool|string>', $e);
}

function arrayUnshiftConstantArray(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,11 @@ public function testBug8389(): void
$this->analyse([__DIR__ . '/data/bug-8389.php'], []);
}

public function testBug8449(): void
{
$this->analyse([__DIR__ . '/data/bug-8449.php'], []);
}

public function testBug5288(): void
{
$this->analyse([__DIR__ . '/data/bug-5288.php'], []);
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-8449.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug8449;

/** @var list<int> $a */
$a = [1];
/** @var list<int> $b */
$b = [2];

array_push($a, ...$b);

/**
* @param list<int> $parameter
*/
function test(array $parameter): void
{
}

test($a);

0 comments on commit 1f421d4

Please sign in to comment.