mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 13:51:54 +01:00
Merge pull request #8619 from kkmuffme/keep-literal-string-for-simple-str_replace
keep literal string for simple str_replace
This commit is contained in:
commit
7c83878ae0
@ -10,6 +10,7 @@ use Psalm\Type\Atomic\TNull;
|
|||||||
use Psalm\Type\Atomic\TString;
|
use Psalm\Type\Atomic\TString;
|
||||||
use Psalm\Type\Union;
|
use Psalm\Type\Union;
|
||||||
|
|
||||||
|
use function call_user_func;
|
||||||
use function count;
|
use function count;
|
||||||
use function in_array;
|
use function in_array;
|
||||||
|
|
||||||
@ -50,7 +51,27 @@ class StrReplaceReturnTypeProvider implements FunctionReturnTypeProviderInterfac
|
|||||||
|
|
||||||
$return_type = Type::getString();
|
$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()]);
|
$return_type = new Union([new TString, new TNull()]);
|
||||||
|
|
||||||
$codebase = $statements_source->getCodebase();
|
$codebase = $statements_source->getCodebase();
|
||||||
|
@ -5,6 +5,8 @@ namespace Psalm\Tests\ReturnTypeProvider;
|
|||||||
use Psalm\Tests\TestCase;
|
use Psalm\Tests\TestCase;
|
||||||
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
||||||
|
|
||||||
|
use function addslashes;
|
||||||
|
|
||||||
use const DIRECTORY_SEPARATOR;
|
use const DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
class DirnameTest extends TestCase
|
class DirnameTest extends TestCase
|
||||||
|
Loading…
x
Reference in New Issue
Block a user