Skip to content

Commit

Permalink
keep literal string for simple str_replace
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Oct 26, 2022
1 parent 4b96c20 commit 720c668
Showing 1 changed file with 19 additions and 1 deletion.
Expand Up @@ -10,6 +10,7 @@
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;

use function call_user_func;
use function count;
use function in_array;

Expand Down Expand Up @@ -50,7 +51,24 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

$return_type = Type::getString();

if (in_array($function_id, ['preg_replace', 'preg_replace_callback'], true)) {
if (in_array($function_id, ['str_replace', 'str_ireplace'], true)
&& $subject_type->isSingleStringLiteral()
) {
$first_arg = $statements_source->node_data->getType($call_args[0]->value);
$second_arg = $statements_source->node_data->getType($call_args[1]->value);
if ($first_arg && $second_arg && $first_arg->isSingleStringLiteral() && $second_arg->isSingleStringLiteral()) {
/**
* @var string $replaced_string
*/
$replaced_string = call_user_func(
$function_id,
$first_arg->getSingleStringLiteral()->value,
$second_arg->getSingleStringLiteral()->value,
$subject_type->getSingleStringLiteral()->value
);
$return_type = Type::getString($replaced_string);
}
} elseif (in_array($function_id, ['preg_replace', 'preg_replace_callback'], true)) {
$return_type = new Union([new TString, new TNull()]);

$codebase = $statements_source->getCodebase();
Expand Down

0 comments on commit 720c668

Please sign in to comment.