Skip to content

Commit

Permalink
String offset access leads non-empty-string (single character)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 14, 2024
1 parent e9c60a2 commit 9439bba
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function getOffsetValueType(Type $offsetType): Type
return new ErrorType();
}

return new StringType();
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}

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

if ($offsetType->isInteger()->yes() || $offsetType instanceof MixedType) {
return new StringType();
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}

return new ErrorType();
Expand Down
22 changes: 11 additions & 11 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public function dataArrayDestructuring(): array
'$secondStringArray',
],
[
'string',
'non-empty-string',
'$thirdStringArray',
],
[
Expand All @@ -1089,43 +1089,43 @@ public function dataArrayDestructuring(): array
'$secondStringArrayList',
],
[
'string',
'non-empty-string',
'$thirdStringArrayList',
],
[
'string',
'$fourthStringArrayList',
],
[
'string',
'non-empty-string',
'$firstStringArrayForeach',
],
[
'string',
'non-empty-string',
'$secondStringArrayForeach',
],
[
'string',
'non-empty-string',
'$thirdStringArrayForeach',
],
[
'string',
'non-empty-string',
'$fourthStringArrayForeach',
],
[
'string',
'non-empty-string',
'$firstStringArrayForeachList',
],
[
'string',
'non-empty-string',
'$secondStringArrayForeachList',
],
[
'string',
'non-empty-string',
'$thirdStringArrayForeachList',
],
[
'string',
'non-empty-string',
'$fourthStringArrayForeachList',
],
[
Expand Down Expand Up @@ -2904,7 +2904,7 @@ public function dataBinaryOperations(): array
'$fooString[4]',
],
[
'string',
'non-empty-string',
'$fooString[$integer]',
],
[
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-3981.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function doFoo(string $s, string $nonEmptyString): void
assertType('false', strtok('', ' '));

assertType('non-empty-string', $nonEmptyString[0]);
assertType('string', $nonEmptyString[1]);
assertType('string', $s[0]);
assertType('non-empty-string', $nonEmptyString[1]);
assertType('non-empty-string', $s[0]);
}

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

public function testBug11506(): void
{
$this->analyse([__DIR__ . '/data/bug-11506.php'], []);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11506.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Bug11506;

function (): void {
$brandName = 'Mercedes-Benz';
$enabledChars = './-';
$enabledCharsCount = \strlen($enabledChars);

for ($i = 0; $i < $enabledCharsCount; $i++) {
$parts = \explode($enabledChars[$i], $brandName);
}
};

0 comments on commit 9439bba

Please sign in to comment.