Skip to content

Commit f8273d5

Browse files
committed
non-empty-array<mixed> does not except array with hasOffsetValue
1 parent 9a0bfd2 commit f8273d5

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public function getConstantStrings(): array
7676

7777
public function accepts(Type $type, bool $strictTypes): AcceptsResult
7878
{
79+
if ($type instanceof HasOffsetType
80+
|| $type instanceof HasOffsetValueType
81+
) {
82+
return AcceptsResult::createYes();
83+
}
84+
7985
if ($type instanceof CompoundType) {
8086
return $type->isAcceptedBy($this, $strictTypes);
8187
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,4 +2053,23 @@ public function testBug7522(): void
20532053
$this->analyse([__DIR__ . '/data/bug-7522.php'], []);
20542054
}
20552055

2056+
public function testBug12847(): void
2057+
{
2058+
$this->checkExplicitMixed = true;
2059+
$this->checkImplicitMixed = true;
2060+
2061+
$this->analyse([__DIR__ . '/data/bug-12847.php'], [
2062+
[
2063+
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
2064+
32,
2065+
'mixed is empty.',
2066+
],
2067+
[
2068+
'Parameter #1 $array of function Bug12847\doSomething expects non-empty-array<mixed>, mixed given.',
2069+
39,
2070+
'mixed is empty.',
2071+
],
2072+
]);
2073+
}
2074+
20562075
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
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+
* @param non-empty-array<mixed> $array
44+
*/
45+
function doSomething(array $array): void
46+
{
47+
48+
}

0 commit comments

Comments
 (0)