Skip to content

Commit 9439bba

Browse files
committed
String offset access leads non-empty-string (single character)
1 parent e9c60a2 commit 9439bba

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

src/Type/StringType.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public function getOffsetValueType(Type $offsetType): Type
7272
return new ErrorType();
7373
}
7474

75-
return new StringType();
75+
return new IntersectionType([
76+
new StringType(),
77+
new AccessoryNonEmptyStringType(),
78+
]);
7679
}
7780

7881
public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
@@ -87,7 +90,10 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
8790
}
8891

8992
if ($offsetType->isInteger()->yes() || $offsetType instanceof MixedType) {
90-
return new StringType();
93+
return new IntersectionType([
94+
new StringType(),
95+
new AccessoryNonEmptyStringType(),
96+
]);
9197
}
9298

9399
return new ErrorType();

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ public function dataArrayDestructuring(): array
10731073
'$secondStringArray',
10741074
],
10751075
[
1076-
'string',
1076+
'non-empty-string',
10771077
'$thirdStringArray',
10781078
],
10791079
[
@@ -1089,43 +1089,43 @@ public function dataArrayDestructuring(): array
10891089
'$secondStringArrayList',
10901090
],
10911091
[
1092-
'string',
1092+
'non-empty-string',
10931093
'$thirdStringArrayList',
10941094
],
10951095
[
10961096
'string',
10971097
'$fourthStringArrayList',
10981098
],
10991099
[
1100-
'string',
1100+
'non-empty-string',
11011101
'$firstStringArrayForeach',
11021102
],
11031103
[
1104-
'string',
1104+
'non-empty-string',
11051105
'$secondStringArrayForeach',
11061106
],
11071107
[
1108-
'string',
1108+
'non-empty-string',
11091109
'$thirdStringArrayForeach',
11101110
],
11111111
[
1112-
'string',
1112+
'non-empty-string',
11131113
'$fourthStringArrayForeach',
11141114
],
11151115
[
1116-
'string',
1116+
'non-empty-string',
11171117
'$firstStringArrayForeachList',
11181118
],
11191119
[
1120-
'string',
1120+
'non-empty-string',
11211121
'$secondStringArrayForeachList',
11221122
],
11231123
[
1124-
'string',
1124+
'non-empty-string',
11251125
'$thirdStringArrayForeachList',
11261126
],
11271127
[
1128-
'string',
1128+
'non-empty-string',
11291129
'$fourthStringArrayForeachList',
11301130
],
11311131
[
@@ -2904,7 +2904,7 @@ public function dataBinaryOperations(): array
29042904
'$fooString[4]',
29052905
],
29062906
[
2907-
'string',
2907+
'non-empty-string',
29082908
'$fooString[$integer]',
29092909
],
29102910
[

tests/PHPStan/Analyser/nsrt/bug-3981.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function doFoo(string $s, string $nonEmptyString): void
1818
assertType('false', strtok('', ' '));
1919

2020
assertType('non-empty-string', $nonEmptyString[0]);
21-
assertType('string', $nonEmptyString[1]);
22-
assertType('string', $s[0]);
21+
assertType('non-empty-string', $nonEmptyString[1]);
22+
assertType('non-empty-string', $s[0]);
2323
}
2424

2525
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -1716,4 +1716,9 @@ public function testCountArrayShift(): void
17161716
$this->analyse([__DIR__ . '/data/count-array-shift.php'], $errors);
17171717
}
17181718

1719+
public function testBug11506(): void
1720+
{
1721+
$this->analyse([__DIR__ . '/data/bug-11506.php'], []);
1722+
}
1723+
17191724
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Bug11506;
4+
5+
function (): void {
6+
$brandName = 'Mercedes-Benz';
7+
$enabledChars = './-';
8+
$enabledCharsCount = \strlen($enabledChars);
9+
10+
for ($i = 0; $i < $enabledCharsCount; $i++) {
11+
$parts = \explode($enabledChars[$i], $brandName);
12+
}
13+
};

0 commit comments

Comments
 (0)