Skip to content

Commit 3cdaab0

Browse files
authored
Regression test
1 parent 966aea1 commit 3cdaab0

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Diff for: tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -2053,4 +2053,35 @@ public function testBug7522(): void
20532053
$this->analyse([__DIR__ . '/data/bug-7522.php'], []);
20542054
}
20552055

2056+
public function testBug12847(): void
2057+
{
2058+
if (PHP_VERSION_ID < 80000) {
2059+
$this->markTestSkipped('Test requires PHP 8.0.');
2060+
}
2061+
2062+
$this->checkExplicitMixed = true;
2063+
$this->checkImplicitMixed = true;
2064+
2065+
$this->analyse([__DIR__ . '/data/bug-12847.php'], [
2066+
[
2067+
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
2068+
32,
2069+
'mixed is empty.',
2070+
],
2071+
[
2072+
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
2073+
39,
2074+
'mixed is empty.',
2075+
],
2076+
[
2077+
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
2078+
61,
2079+
],
2080+
[
2081+
'Parameter #1 $array of function Bug12847\doSomethingWithInt expects non-empty-array<int>, non-empty-array given.',
2082+
67,
2083+
],
2084+
]);
2085+
}
2086+
20562087
}

Diff for: tests/PHPStan/Rules/Functions/data/bug-12847.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12847;
4+
5+
function doBar():void {
6+
/**
7+
* @var array<mixed> $array
8+
*/
9+
$array = [
10+
'abc' => 'def'
11+
];
12+
13+
if (isset($array['def'])) {
14+
doSomething($array);
15+
}
16+
}
17+
18+
function doFoo(array $array):void {
19+
if (isset($array['def'])) {
20+
doSomething($array);
21+
}
22+
}
23+
24+
function doFooBar(array $array):void {
25+
if (array_key_exists('foo', $array) && $array['foo'] === 17) {
26+
doSomething($array);
27+
}
28+
}
29+
30+
function doImplicitMixed($mixed):void {
31+
if (isset($mixed['def'])) {
32+
doSomething($mixed);
33+
}
34+
}
35+
36+
function doExplicitMixed(mixed $mixed): void
37+
{
38+
if (isset($mixed['def'])) {
39+
doSomething($mixed);
40+
}
41+
}
42+
43+
/**
44+
* @param non-empty-array<mixed> $array
45+
*/
46+
function doSomething(array $array): void
47+
{
48+
49+
}
50+
51+
/**
52+
* @param non-empty-array<int> $array
53+
*/
54+
function doSomethingWithInt(array $array): void
55+
{
56+
57+
}
58+
59+
function doFooBarInt(array $array):void {
60+
if (array_key_exists('foo', $array) && $array['foo'] === 17) {
61+
doSomethingWithInt($array); // expect error, because our array is not sealed
62+
}
63+
}
64+
65+
function doFooBarString(array $array):void {
66+
if (array_key_exists('foo', $array) && $array['foo'] === "hello") {
67+
doSomethingWithInt($array); // expect error, because our array is not sealed
68+
}
69+
}

0 commit comments

Comments
 (0)