Skip to content

Commit b84ad84

Browse files
committed
More precise implode() return type
1 parent 14faee1 commit b84ad84

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/Type/Php/ImplodeFunctionReturnTypeExtension.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,23 @@ private function implode(Type $arrayType, Type $separatorType): Type
8181
}
8282

8383
$accessoryTypes = [];
84+
$valueTypeAsString = $arrayType->getIterableValueType()->toString();
8485
if ($arrayType->isIterableAtLeastOnce()->yes()) {
85-
if ($arrayType->getIterableValueType()->isNonFalsyString()->yes() || $separatorType->isNonFalsyString()->yes()) {
86+
if ($valueTypeAsString->isNonFalsyString()->yes() || $separatorType->isNonFalsyString()->yes()) {
8687
$accessoryTypes[] = new AccessoryNonFalsyStringType();
87-
} elseif ($arrayType->getIterableValueType()->isNonEmptyString()->yes() || $separatorType->isNonEmptyString()->yes()) {
88+
} elseif ($valueTypeAsString->isNonEmptyString()->yes() || $separatorType->isNonEmptyString()->yes()) {
8889
$accessoryTypes[] = new AccessoryNonEmptyStringType();
8990
}
9091
}
9192

9293
// implode is one of the four functions that can produce literal strings as blessed by the original RFC: wiki.php.net/rfc/is_literal
93-
if ($arrayType->getIterableValueType()->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) {
94+
if ($valueTypeAsString->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) {
9495
$accessoryTypes[] = new AccessoryLiteralStringType();
9596
}
96-
if ($arrayType->getIterableValueType()->isLowercaseString()->yes() && $separatorType->isLowercaseString()->yes()) {
97+
if ($valueTypeAsString->isLowercaseString()->yes() && $separatorType->isLowercaseString()->yes()) {
9798
$accessoryTypes[] = new AccessoryLowercaseStringType();
9899
}
99-
if ($arrayType->getIterableValueType()->isUppercaseString()->yes() && $separatorType->isUppercaseString()->yes()) {
100+
if ($valueTypeAsString->isUppercaseString()->yes() && $separatorType->isUppercaseString()->yes()) {
100101
$accessoryTypes[] = new AccessoryUppercaseStringType();
101102
}
102103

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function returnsBool(): bool {
4141
assertType('string', $s);
4242

4343
$s = sprintf("%s", implode(', ', array_map('intval', returnsArray())));
44-
assertType('string', $s);
44+
assertType('lowercase-string&uppercase-string', $s);
4545

4646
$s = sprintf('%2$s', 1234, returnsNonFalsyString());
4747
assertType('non-falsy-string', $s);

tests/PHPStan/Analyser/nsrt/implode.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
class Foo
88
{
9+
/**
10+
* @param array<int> $arr
11+
*/
12+
public function ints(array $arr, int $i)
13+
{
14+
assertType("lowercase-string&uppercase-string", implode($arr));
15+
assertType("lowercase-string&non-empty-string&uppercase-string", implode([$i, $i]));
16+
if ($i !== 0) {
17+
assertType("lowercase-string&non-falsy-string&uppercase-string", implode([$i, $i]));
18+
}
19+
}
20+
921
const X = 'x';
1022
const ONE = 1;
1123

tests/PHPStan/Analyser/nsrt/non-empty-string.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function sayHello(int $i): void
212212
// coming from issue #5291
213213
$s = array(1, $i);
214214

215-
assertType('non-falsy-string', implode("a", $s));
215+
assertType('lowercase-string&non-falsy-string', implode("a", $s));
216216
}
217217

218218
/**
@@ -233,7 +233,7 @@ public function sayHello2(int $i): void
233233
// coming from issue #5291
234234
$s = array(1, $i);
235235

236-
assertType('non-falsy-string', join("a", $s));
236+
assertType('lowercase-string&non-falsy-string', join("a", $s));
237237
}
238238

239239
/**

0 commit comments

Comments
 (0)