Skip to content

Commit e471895

Browse files
Improve
1 parent 14649b5 commit e471895

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/Php/PhpVersion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function deprecatesDynamicProperties(): bool
236236
return $this->versionId >= 80200;
237237
}
238238

239-
public function strSplitReturnsEmptyArray(): bool
239+
public function strSplitReturnsEmptyArrayOnEmptyString(): bool
240240
{
241241
return $this->versionId >= 80200;
242242
}

src/Type/Php/StrSplitFunctionReturnTypeExtension.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,16 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
9191
if (count($constantStrings) > 0) {
9292
$results = [];
9393
foreach ($constantStrings as $constantString) {
94-
$items = $encoding === null
95-
? str_split($constantString->getValue(), $splitLength)
96-
: @mb_str_split($constantString->getValue(), $splitLength, $encoding);
94+
$value = $constantString->getValue();
95+
96+
if ($encoding === null && $value === '') {
97+
// Simulate the str_split call with the analysed PHP Version instead of the runtime one.
98+
$items = $this->phpVersion->strSplitReturnsEmptyArrayOnEmptyString() ? [] : [''];
99+
} else {
100+
$items = $encoding === null
101+
? str_split($value, $splitLength)
102+
: @mb_str_split($value, $splitLength, $encoding);
103+
}
97104

98105
$results[] = self::createConstantArrayFrom($items, $scope);
99106
}
@@ -104,7 +111,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
104111

105112
$isInputNonEmptyString = $stringType->isNonEmptyString()->yes();
106113

107-
if ($isInputNonEmptyString || $this->phpVersion->strSplitReturnsEmptyArray()) {
114+
if ($isInputNonEmptyString || $this->phpVersion->strSplitReturnsEmptyArrayOnEmptyString()) {
108115
$returnValueType = TypeCombinator::intersect(new StringType(), new AccessoryNonEmptyStringType());
109116
} else {
110117
$returnValueType = new StringType();
@@ -115,7 +122,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
115122
// Non-empty-string will return an array with at least an element
116123
$isInputNonEmptyString
117124
// str_split('', 1) returns [''] on old PHP version and [] on new ones
118-
|| ($functionReflection->getName() === 'str_split' && !$this->phpVersion->strSplitReturnsEmptyArray())
125+
|| ($functionReflection->getName() === 'str_split' && !$this->phpVersion->strSplitReturnsEmptyArrayOnEmptyString())
119126
) {
120127
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType());
121128
}

0 commit comments

Comments
 (0)