Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
if ($replaceArgumentType->isArray()->yes()) {
$replaceArgumentType = $replaceArgumentType->getIterableValueType();
}
} elseif ($functionReflection->getName() === 'strtr' && isset($functionCall->getArgs()[1])) {
// `strtr` has two signatures: `strtr($string1, $string2, $string3)` and `strtr($string1, $array)`
$secondArgumentType = $scope->getType($functionCall->getArgs()[1]->value);
if ($secondArgumentType->isArray()->yes()) {
$replaceArgumentType = $secondArgumentType->getIterableValueType();
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/nsrt/strtr.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ function doFoo(string $s, $nonEmptyString, $nonFalseyString) {
assertType('non-empty-string', strtr($nonFalseyString, $s, $nonEmptyString));
assertType('non-falsy-string', strtr($nonFalseyString, $nonEmptyString, $nonFalseyString));
assertType('non-falsy-string', strtr($nonFalseyString, $nonFalseyString, $nonFalseyString));

assertType('string', strtr($s, [$s => $nonEmptyString]));
assertType('string', strtr($s, [$nonEmptyString => $nonEmptyString]));
assertType('string', strtr($s, [$nonFalseyString => $nonFalseyString]));

assertType('non-empty-string', strtr($nonEmptyString, [$s => $nonEmptyString]));
assertType('non-empty-string', strtr($nonEmptyString, [$nonEmptyString => $nonEmptyString]));
assertType('non-empty-string', strtr($nonEmptyString, [$nonFalseyString => $nonFalseyString]));

assertType('non-empty-string', strtr($nonFalseyString, [$s => $nonEmptyString]));
assertType('non-falsy-string', strtr($nonFalseyString, [$nonEmptyString => $nonFalseyString]));
assertType('non-falsy-string', strtr($nonFalseyString, [$nonFalseyString => $nonFalseyString]));
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3656,6 +3656,16 @@ public function testBug5642(): void
]);
}

public function testBug13708(): void
{
$this->checkThisOnly = false;
$this->checkNullables = false;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;

$this->analyse([__DIR__ . '/data/bug-13708.php'], []);
}

public function testBug3396(): void
{
$this->checkThisOnly = false;
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-13708.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace Bug13708;

class HelloWorld
{
/**
* @param non-empty-string $s
*/
public function takeNonEmpty(string $s): void
{
return;
}

public function doStuff(): void
{
$this->takeNonEmpty(
strtr('change {me}', ['{me}' => 'me'])
);
}
}
Loading