Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
array_push
with an empty constant array
- Loading branch information
1 parent
06a5c26
commit 20539b3
Showing
6 changed files
with
230 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace ArrayPush; | ||
|
||
use function array_push; | ||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @param string[] $a | ||
* @param int[] $b | ||
* @param non-empty-array<int> $c | ||
* @param array<int|string> $d | ||
*/ | ||
function arrayPush(array $a, array $b, array $c, array $d): void | ||
{ | ||
array_push($a, ...$b); | ||
assertType('non-empty-array<int|string>', $a); | ||
|
||
array_push($b, ...[]); | ||
assertType('array<int>', $b); | ||
|
||
array_push($c, ...[19, 'baz', false]); | ||
assertType('non-empty-array<\'baz\'|int|false>', $c); | ||
|
||
/** @var array<bool|null> $d1 */ | ||
$d1 = []; | ||
array_push($d, ...$d1); | ||
assertType('non-empty-array<bool|int|string|null>', $d); | ||
} | ||
|
||
function arrayPushConstantArray(): void | ||
{ | ||
$a = ['foo' => 17, 'a', 'bar' => 18,]; | ||
array_push($a, ...[19, 'baz', false]); | ||
assertType('array{foo: 17, 0: \'a\', bar: 18, 1: 19, 2: \'baz\', 3: false}', $a); | ||
|
||
$b = ['foo' => 17, 'a', 'bar' => 18]; | ||
array_push($b, 19, 'baz', false); | ||
assertType('array{foo: 17, 0: \'a\', bar: 18, 1: 19, 2: \'baz\', 3: false}', $b); | ||
|
||
$c = ['foo' => 17, 'a', 'bar' => 18]; | ||
array_push($c, ...[]); | ||
assertType('array{foo: 17, 0: \'a\', bar: 18}', $c); | ||
|
||
$d = []; | ||
array_push($d, ...[]); | ||
assertType('array{}', $d); | ||
|
||
$e = []; | ||
array_push($e, 19, 'baz', false); | ||
assertType('array{19, \'baz\', false}', $e); | ||
|
||
$f = [17]; | ||
/** @var array<bool|null> $f1 */ | ||
$f1 = []; | ||
array_push($f, ...$f1); | ||
assertType('non-empty-array<int, bool|int|null>', $f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace ArrayUnshift; | ||
|
||
use function array_unshift; | ||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @param string[] $a | ||
* @param int[] $b | ||
* @param non-empty-array<int> $c | ||
* @param array<int|string> $d | ||
*/ | ||
function arrayUnshift(array $a, array $b, array $c, array $d): void | ||
{ | ||
array_unshift($a, ...$b); | ||
assertType('non-empty-array<int|string>', $a); | ||
|
||
array_unshift($b, ...[]); | ||
assertType('array<int>', $b); | ||
|
||
array_unshift($c, ...[19, 'baz', false]); | ||
assertType('non-empty-array<\'baz\'|int|false>', $c); | ||
|
||
/** @var array<bool|null> $d1 */ | ||
$d1 = []; | ||
array_unshift($d, ...$d1); | ||
assertType('non-empty-array<bool|int|string|null>', $d); | ||
} | ||
|
||
function arrayUnshiftConstantArray(): void | ||
{ | ||
$a = ['foo' => 17, 'a', 'bar' => 18,]; | ||
array_unshift($a, ...[19, 'baz', false]); | ||
assertType('array{0: 19, 1: \'baz\', 2: false, foo: 17, 3: \'a\', bar: 18}', $a); | ||
|
||
$b = ['foo' => 17, 'a', 'bar' => 18]; | ||
array_unshift($b, 19, 'baz', false); | ||
assertType('array{0: 19, 1: \'baz\', 2: false, foo: 17, 3: \'a\', bar: 18}', $b); | ||
|
||
$c = ['foo' => 17, 'a', 'bar' => 18]; | ||
array_unshift($c, ...[]); | ||
assertType('array{foo: 17, 0: \'a\', bar: 18}', $c); | ||
|
||
$d = []; | ||
array_unshift($d, ...[]); | ||
assertType('array{}', $d); | ||
|
||
$e = []; | ||
array_unshift($e, 19, 'baz', false); | ||
assertType('array{19, \'baz\', false}', $e); | ||
|
||
$f = [17]; | ||
/** @var array<bool|null> $f1 */ | ||
$f1 = []; | ||
array_unshift($f, ...$f1); | ||
assertType('non-empty-array<int, bool|int|null>', $f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug6974; | ||
|
||
class A | ||
{ | ||
public function test1(): void | ||
{ | ||
$a = []; | ||
$b = []; | ||
array_push($a, ...$b); | ||
$c = empty($a) ? 'empty' : 'non-empty'; | ||
} | ||
|
||
public function test2(): void | ||
{ | ||
$a = []; | ||
/** @var mixed[] $b */ | ||
$b = []; | ||
array_push($a, ...$b); | ||
$c = empty($a) ? 'empty' : 'non-empty'; | ||
} | ||
|
||
public function test3(): void | ||
{ | ||
$a = []; | ||
/** @var non-empty-array<mixed> $b */ | ||
$b = []; | ||
array_push($a, ...$b); | ||
$c = empty($a) ? 'empty' : 'non-empty'; | ||
} | ||
} |