Skip to content

Commit b0aa47e

Browse files
committed
Revert "Fix"
This reverts commit 89ee746.
1 parent 3affe77 commit b0aa47e

File tree

6 files changed

+44
-111
lines changed

6 files changed

+44
-111
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
use PHPStan\Rules\RuleLevelHelper;
1313
use PHPStan\Type\BenevolentUnionType;
1414
use PHPStan\Type\ErrorType;
15-
use PHPStan\Type\NeverType;
1615
use PHPStan\Type\Type;
17-
use PHPStan\Type\TypeCombinator;
1816
use PHPStan\Type\TypeUtils;
1917
use PHPStan\Type\VerbosityLevel;
2018
use function count;
@@ -62,14 +60,6 @@ public function check(
6260
}
6361

6462
if ($type->hasOffsetValueType($dimType)->no()) {
65-
if ($type->isArray()->yes()) {
66-
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
67-
if ($validArrayDimType instanceof NeverType) {
68-
// Already reported by InvalidKeyInArrayDimFetchRule
69-
return [];
70-
}
71-
}
72-
7363
return [
7464
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
7565
->identifier('offsetAccess.notFound')
@@ -86,32 +76,28 @@ public function check(
8676
$flattenedTypes = TypeUtils::flattenTypes($type);
8777
}
8878

89-
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
90-
9179
foreach ($flattenedTypes as $innerType) {
92-
$dimTypeToCheck = $innerType->isArray()->yes() ? $validArrayDimType : $dimType;
93-
9480
if (
9581
$this->reportPossiblyNonexistentGeneralArrayOffset
9682
&& $innerType->isArray()->yes()
9783
&& !$innerType->isConstantArray()->yes()
98-
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
84+
&& !$innerType->hasOffsetValueType($dimType)->yes()
9985
) {
10086
$report = true;
10187
break;
10288
}
10389
if (
10490
$this->reportPossiblyNonexistentConstantArrayOffset
10591
&& $innerType->isConstantArray()->yes()
106-
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
92+
&& !$innerType->hasOffsetValueType($dimType)->yes()
10793
) {
10894
$report = true;
10995
break;
11096
}
111-
if ($dimTypeToCheck instanceof BenevolentUnionType) {
112-
$flattenedInnerTypes = [$dimTypeToCheck];
97+
if ($dimType instanceof BenevolentUnionType) {
98+
$flattenedInnerTypes = [$dimType];
11399
} else {
114-
$flattenedInnerTypes = TypeUtils::flattenTypes($dimTypeToCheck);
100+
$flattenedInnerTypes = TypeUtils::flattenTypes($dimType);
115101
}
116102
foreach ($flattenedInnerTypes as $innerDimType) {
117103
if (

tests/PHPStan/Levels/data/arrayOffsetAccess-3.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
"message": "Invalid array key type DateTimeImmutable.",
44
"line": 17,
55
"ignorable": true
6+
},
7+
{
8+
"message": "Offset DateTimeImmutable does not exist on array.",
9+
"line": 17,
10+
"ignorable": true
611
}
712
]

tests/PHPStan/Levels/data/arrayOffsetAccess-7.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
[
2+
{
3+
"message": "Offset int|object might not exist on array.",
4+
"line": 19,
5+
"ignorable": true
6+
},
27
{
38
"message": "Possibly invalid array key type int|object.",
49
"line": 19,
510
"ignorable": true
611
},
12+
{
13+
"message": "Offset object|null might not exist on array.",
14+
"line": 20,
15+
"ignorable": true
16+
},
717
{
818
"message": "Possibly invalid array key type object|null.",
919
"line": 20,
1020
"ignorable": true
1121
},
22+
{
23+
"message": "Offset DateTimeImmutable might not exist on array|ArrayAccess.",
24+
"line": 26,
25+
"ignorable": true
26+
},
1227
{
1328
"message": "Possibly invalid array key type DateTimeImmutable.",
1429
"line": 26,
1530
"ignorable": true
1631
},
32+
{
33+
"message": "Offset int|object might not exist on array|ArrayAccess.",
34+
"line": 28,
35+
"ignorable": true
36+
},
1737
{
1838
"message": "Possibly invalid array key type int|object.",
1939
"line": 28,
2040
"ignorable": true
2141
},
42+
{
43+
"message": "Offset object|null might not exist on array|ArrayAccess.",
44+
"line": 29,
45+
"ignorable": true
46+
},
2247
{
2348
"message": "Possibly invalid array key type object|null.",
2449
"line": 29,

tests/PHPStan/Levels/data/stringOffsetAccess-3.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@
1818
"message": "Invalid array key type stdClass.",
1919
"line": 59,
2020
"ignorable": true
21+
},
22+
{
23+
"message": "Offset stdClass does not exist on array{baz: 21}|array{foo: 17, bar: 19}.",
24+
"line": 59,
25+
"ignorable": true
2126
}
2227
]

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,10 @@ public function testBugObject(): void
949949
'Offset int|object does not exist on array{baz: 21}|array{foo: 17, bar: 19}.',
950950
12,
951951
],
952+
[
953+
'Offset object does not exist on array<string, int>.',
954+
21,
955+
],
952956
]);
953957
}
954958

@@ -1045,55 +1049,6 @@ public function testBug13538(): void
10451049
]);
10461050
}
10471051

1048-
public function testPR4385(): void
1049-
{
1050-
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
1051-
$this->reportPossiblyNonexistentConstantArrayOffset = true;
1052-
1053-
$this->analyse([__DIR__ . '/data/pr-4385.php'], [
1054-
[
1055-
'Offset int might not exist on array<int>.',
1056-
24,
1057-
],
1058-
[
1059-
'Offset string might not exist on array<int>.',
1060-
25,
1061-
],
1062-
[
1063-
'Offset array<int>|int might not exist on array<int>.',
1064-
28,
1065-
],
1066-
[
1067-
'Offset array<int>|string might not exist on array<int>.',
1068-
29,
1069-
],
1070-
[
1071-
'Offset 0|array<int> might not exist on array<int>.',
1072-
30,
1073-
],
1074-
[
1075-
'Offset int might not exist on array{string}.',
1076-
33,
1077-
],
1078-
[
1079-
'Offset string might not exist on array{string}.',
1080-
34,
1081-
],
1082-
[
1083-
'Offset array<int>|int might not exist on array{string}.',
1084-
37,
1085-
],
1086-
[
1087-
'Offset array<int>|string might not exist on array{string}.',
1088-
38,
1089-
],
1090-
[
1091-
'Offset array<int>|int might not exist on array<int>|string.',
1092-
41,
1093-
],
1094-
]);
1095-
}
1096-
10971052
public function testBug12805(): void
10981053
{
10991054
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

tests/PHPStan/Rules/Arrays/data/pr-4385.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)