Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
Expand Down Expand Up @@ -76,6 +75,7 @@ public function check(
} else {
$flattenedTypes = TypeUtils::flattenTypes($type);
}

foreach ($flattenedTypes as $innerType) {
if (
$this->reportPossiblyNonexistentGeneralArrayOffset
Expand All @@ -94,15 +94,15 @@ public function check(
$report = true;
break;
}
if ($dimType instanceof UnionType) {
if ($innerType->hasOffsetValueType($dimType)->no()) {
$report = true;
break;
}
continue;
if ($dimType instanceof BenevolentUnionType) {
$flattenedInnerTypes = [$dimType];
} else {
$flattenedInnerTypes = TypeUtils::flattenTypes($dimType);
}
foreach (TypeUtils::flattenTypes($dimType) as $innerDimType) {
if ($innerType->hasOffsetValueType($innerDimType)->no()) {
foreach ($flattenedInnerTypes as $innerDimType) {
if (
$innerType->hasOffsetValueType($innerDimType)->no()
) {
$report = true;
break 2;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Levels/data/stringOffsetAccess-7.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"message": "Offset int|object might not exist on 'foo'.",
"line": 19,
"ignorable": true
},
{
"message": "Offset 'foo' might not exist on array|string.",
"line": 27,
Expand All @@ -9,6 +14,11 @@
"line": 31,
"ignorable": true
},
{
"message": "Offset int|object might not exist on array|string.",
"line": 35,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 35,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function testRule(): void
'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).',
258,
],
[
'Offset int|null might not exist on array<int, string>.',
309,
],
[
'Offset null does not exist on array<int, string>.',
310,
Expand All @@ -127,6 +131,10 @@ public function testRule(): void
'Offset int does not exist on array<string, string>.',
312,
],
[
'Offset int|null might not exist on array<string, string>.',
314,
],
[
'Offset \'baz\' might not exist on array{bar: 1, baz?: 2}.',
344,
Expand Down Expand Up @@ -185,6 +193,10 @@ public function testStrings(): void
'Offset 12.34 does not exist on \'foo\'.',
13,
],
[
'Offset int|object might not exist on \'foo\'.',
16,
],
[
'Offset \'foo\' might not exist on array|string.',
24,
Expand All @@ -193,6 +205,10 @@ public function testStrings(): void
'Offset 12.34 might not exist on array|string.',
28,
],
[
'Offset int|object might not exist on array|string.',
32,
],
]);
}

Expand Down Expand Up @@ -949,4 +965,14 @@ public function testBug12447(): void
]);
}

public function testBug1061(): void
{
$this->analyse([__DIR__ . '/data/bug-1061.php'], [
[
"Offset 'one'|'two' might not exist on array{two: 1, three: 2}.",
14,
],
]);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-1061.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Bug1061;

class A {
const KEYS = ["one", "two"];
const ARR = [
"two" => 1,
"three" => 2
];
}

foreach (A::KEYS as $key) {
echo A::ARR[$key];
}
Loading