diff --git a/src/Type/StringType.php b/src/Type/StringType.php index 3fe7dfca68..5fe9ae444a 100644 --- a/src/Type/StringType.php +++ b/src/Type/StringType.php @@ -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 @@ -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(); diff --git a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php index e165bbb820..08cbf6d7a5 100644 --- a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php @@ -1073,7 +1073,7 @@ public function dataArrayDestructuring(): array '$secondStringArray', ], [ - 'string', + 'non-empty-string', '$thirdStringArray', ], [ @@ -1089,7 +1089,7 @@ public function dataArrayDestructuring(): array '$secondStringArrayList', ], [ - 'string', + 'non-empty-string', '$thirdStringArrayList', ], [ @@ -1097,35 +1097,35 @@ public function dataArrayDestructuring(): array '$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', ], [ @@ -2904,7 +2904,7 @@ public function dataBinaryOperations(): array '$fooString[4]', ], [ - 'string', + 'non-empty-string', '$fooString[$integer]', ], [ diff --git a/tests/PHPStan/Analyser/nsrt/bug-3981.php b/tests/PHPStan/Analyser/nsrt/bug-3981.php index 5656b80018..0c12e24089 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-3981.php +++ b/tests/PHPStan/Analyser/nsrt/bug-3981.php @@ -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]); } } diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 90859848d0..b9368eef46 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-11506.php b/tests/PHPStan/Rules/Functions/data/bug-11506.php new file mode 100644 index 0000000000..8fbece54ea --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-11506.php @@ -0,0 +1,13 @@ +