Skip to content

Commit fb4e0a1

Browse files
Fix strtr inferences
1 parent 389abe8 commit fb4e0a1

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
9898
if ($replaceArgumentType->isArray()->yes()) {
9999
$replaceArgumentType = $replaceArgumentType->getIterableValueType();
100100
}
101+
} elseif ($functionReflection->getName() === 'strtr' && isset($functionCall->getArgs()[1])) {
102+
// `strtr` has two signatures: `strtr($string1, $string2, $string3)` and `strtr($string1, $array)`
103+
$secondArgumentType = $scope->getType($functionCall->getArgs()[1]->value);
104+
if ($secondArgumentType->isArray()->yes()) {
105+
$replaceArgumentType = $secondArgumentType->getIterableValueType();
106+
}
101107
}
102108
}
103109

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3656,6 +3656,16 @@ public function testBug5642(): void
36563656
]);
36573657
}
36583658

3659+
public function testBug13708(): void
3660+
{
3661+
$this->checkThisOnly = false;
3662+
$this->checkNullables = false;
3663+
$this->checkUnionTypes = true;
3664+
$this->checkExplicitMixed = false;
3665+
3666+
$this->analyse([__DIR__ . '/data/bug-13708.php'], []);
3667+
}
3668+
36593669
public function testBug3396(): void
36603670
{
36613671
$this->checkThisOnly = false;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13708;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param non-empty-string $s
11+
*/
12+
public function takeNonEmpty(string $s): void
13+
{
14+
return;
15+
}
16+
17+
public function doStuff(): void
18+
{
19+
$this->takeNonEmpty(
20+
strtr('change {me}', ['{me}' => 'me'])
21+
);
22+
}
23+
}

0 commit comments

Comments
 (0)