Skip to content

Commit 2abb92e

Browse files
committed
NonexistentOffsetInArrayDimFetchCheck - be less strict about BenevolentUnionType
1 parent b0cc11f commit 2abb92e

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\RuleError;
88
use PHPStan\Rules\RuleErrorBuilder;
99
use PHPStan\Rules\RuleLevelHelper;
10+
use PHPStan\Type\BenevolentUnionType;
1011
use PHPStan\Type\ErrorType;
1112
use PHPStan\Type\Type;
1213
use PHPStan\Type\TypeUtils;
@@ -68,7 +69,12 @@ static function (Type $type) use ($dimType): bool {
6869
}
6970

7071
if (!$report && $this->reportMaybes) {
71-
foreach (TypeUtils::flattenTypes($type) as $innerType) {
72+
if ($type instanceof BenevolentUnionType) {
73+
$flattenedTypes = [$type];
74+
} else {
75+
$flattenedTypes = TypeUtils::flattenTypes($type);
76+
}
77+
foreach ($flattenedTypes as $innerType) {
7278
if ($dimType instanceof UnionType) {
7379
if ($innerType->hasOffsetValueType($dimType)->no()) {
7480
$report = true;

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,14 @@ public function testBug2689(): void
268268
]);
269269
}
270270

271+
public function testBug5169(): void
272+
{
273+
$this->analyse([__DIR__ . '/data/bug-5169.php'], [
274+
[
275+
'Cannot access offset mixed on (float|int).',
276+
29,
277+
],
278+
]);
279+
}
280+
271281
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug5169;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param array<mixed> $configs
9+
*
10+
* @return array<mixed>
11+
*/
12+
protected function merge(array $configs): array
13+
{
14+
$result = [];
15+
foreach ($configs as $config) {
16+
$result += $config;
17+
18+
foreach ($config as $name => $dto) {
19+
$result[$name] += $dto;
20+
}
21+
}
22+
23+
return $result;
24+
}
25+
26+
public function merge2($mixed): void
27+
{
28+
$f = $mixed - $mixed;
29+
$f[$mixed] = true;
30+
}
31+
}

0 commit comments

Comments
 (0)