Skip to content

Commit 91a494c

Browse files
authored
More precise string functions return type with replacement array
1 parent 4529113 commit 91a494c

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
8989

9090
if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
9191
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);
92+
if ($replaceArgumentType->isArray()->yes()) {
93+
$replaceArgumentType = $replaceArgumentType->getIterableValueType();
94+
}
9295

9396
$accessories = [];
9497
if ($subjectArgumentType->isNonFalsyString()->yes() && $replaceArgumentType->isNonFalsyString()->yes()) {

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -1246,4 +1246,14 @@ public function testBug4443(): void
12461246
]);
12471247
}
12481248

1249+
public function testBug12928(): void
1250+
{
1251+
$this->analyse([__DIR__ . '/data/bug-12928.php'], [
1252+
[
1253+
'Method Bug12928\FooBarBaz::render() should return non-empty-string but returns string.',
1254+
59,
1255+
],
1256+
]);
1257+
}
1258+
12491259
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Bug12928;
4+
5+
class Foo {
6+
/**
7+
* @param non-empty-string $phptFile
8+
* @param non-empty-string $code
9+
*
10+
* @return non-empty-string
11+
*/
12+
public function render(string $phptFile, string $code): string
13+
{
14+
return str_replace(
15+
[
16+
'__DIR__',
17+
'__FILE__',
18+
],
19+
[
20+
"'" . dirname($phptFile) . "'",
21+
"'" . $phptFile . "'",
22+
],
23+
$code,
24+
);
25+
}
26+
}
27+
28+
class FooBar {
29+
/**
30+
* @param non-empty-string $phptFile
31+
* @param non-falsy-string $code
32+
* @param array<non-falsy-string> $replace
33+
*
34+
* @return non-falsy-string
35+
*/
36+
public function render(string $code, array $replace): string
37+
{
38+
return str_replace(
39+
[
40+
'__DIR__',
41+
'__FILE__',
42+
],
43+
$replace,
44+
$code,
45+
);
46+
}
47+
}
48+
49+
50+
class FooBarBaz {
51+
/**
52+
* @param non-empty-string $phptFile
53+
* @param non-empty-string $code
54+
*
55+
* @return non-empty-string
56+
*/
57+
public function render(string $code, array $replace): string
58+
{
59+
return str_replace(
60+
[
61+
'__DIR__',
62+
'__FILE__',
63+
],
64+
$replace,
65+
$code,
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)