Skip to content

Commit 9cd58b5

Browse files
authored
More precise implode() return type
1 parent c9c74c5 commit 9cd58b5

8 files changed

+40
-14
lines changed

src/Type/Php/ImplodeFunctionReturnTypeExtension.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ 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
}
@@ -93,10 +94,10 @@ private function implode(Type $arrayType, Type $separatorType): Type
9394
if ($arrayType->getIterableValueType()->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

src/Type/Php/StrCaseFunctionsReturnTypeExtension.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,19 @@ public function getTypeFromFunctionCall(
142142
}
143143

144144
$accessoryTypes = [];
145-
if ($forceLowercase || ($keepLowercase && $argType->isLowercaseString()->yes())) {
145+
$argStringType = $argType->toString();
146+
if ($forceLowercase || ($keepLowercase && $argStringType->isLowercaseString()->yes())) {
146147
$accessoryTypes[] = new AccessoryLowercaseStringType();
147148
}
148-
if ($forceUppercase || ($keepUppercase && $argType->isUppercaseString()->yes())) {
149+
if ($forceUppercase || ($keepUppercase && $argStringType->isUppercaseString()->yes())) {
149150
$accessoryTypes[] = new AccessoryUppercaseStringType();
150151
}
151152

152-
if ($argType->isNumericString()->yes()) {
153+
if ($argStringType->isNumericString()->yes()) {
153154
$accessoryTypes[] = new AccessoryNumericStringType();
154-
} elseif ($argType->isNonFalsyString()->yes()) {
155+
} elseif ($argStringType->isNonFalsyString()->yes()) {
155156
$accessoryTypes[] = new AccessoryNonFalsyStringType();
156-
} elseif ($argType->isNonEmptyString()->yes()) {
157+
} elseif ($argStringType->isNonEmptyString()->yes()) {
157158
$accessoryTypes[] = new AccessoryNonEmptyStringType();
158159
}
159160

src/Type/Php/StrContainingTypeSpecifyingExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
6666
[$hackstackArg, $needleArg] = self::STR_CONTAINING_FUNCTIONS[strtolower($functionReflection->getName())];
6767

6868
$haystackType = $scope->getType($args[$hackstackArg]->value);
69-
$needleType = $scope->getType($args[$needleArg]->value);
69+
$needleType = $scope->getType($args[$needleArg]->value)->toString();
7070

7171
if ($needleType->isNonEmptyString()->yes() && $haystackType->isString()->yes()) {
7272
$accessories = [

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

+13-1
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

@@ -49,6 +61,6 @@ public function constArrays5($constArr) {
4961

5062
/** @param array{0: 1, 1: 'a'|'b', 3?: 'c'|'d', 4?: 'e'|'f', 5?: 'g'|'h', 6?: 'x'|'y'} $constArr */
5163
public function constArrays6($constArr) {
52-
assertType("string", implode('', $constArr));
64+
assertType("lowercase-string&non-falsy-string", implode('', $constArr));
5365
}
5466
}

tests/PHPStan/Analyser/nsrt/non-empty-string-str-containing-fns.php

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ class Foo {
1414
*/
1515
public function strContains(string $s, string $s2, $nonES, $nonFalsy, $numS, $literalS, $nonEAndNumericS, int $i): void
1616
{
17+
if (str_contains($i, 0)) {
18+
assertType('int', $i);
19+
}
20+
if (str_contains($s, 0)) {
21+
assertType('non-empty-string', $s);
22+
}
23+
if (str_contains($s, 1)) {
24+
assertType('non-falsy-string', $s);
25+
}
26+
1727
if (str_contains($s, ':')) {
1828
assertType('non-falsy-string', $s);
1929
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ 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));
216+
assertType('non-falsy-string&uppercase-string', implode("A", $s));
216217
}
217218

218219
/**
@@ -233,7 +234,7 @@ public function sayHello2(int $i): void
233234
// coming from issue #5291
234235
$s = array(1, $i);
235236

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

239240
/**

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

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function stringFunctions(string $s, $nonFalsey, $arrayOfNonFalsey, $nonEmptyArra
8787
assertType('non-falsy-string', escapeshellarg($nonFalsey));
8888
assertType('non-falsy-string', escapeshellcmd($nonFalsey));
8989

90+
assertType('non-falsy-string&uppercase-string', strtoupper($s ?: 1));
9091
assertType('non-falsy-string&uppercase-string', strtoupper($nonFalsey));
9192
assertType('lowercase-string&non-falsy-string', strtolower($nonFalsey));
9293
assertType('non-falsy-string&uppercase-string', mb_strtoupper($nonFalsey));

0 commit comments

Comments
 (0)