1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Infer mb_strtolower() result as string when encoding is specified

`mb_strtolower()` may return characters we generally consider uppercase
when it's given the encoding argument. This PR makes Psalm to err on the
side of caution and treat the return type as `string` rather than
`lowercase-string` in this case

Refs vimeo/psalm#6908
This commit is contained in:
Bruce Weirdan 2021-11-14 03:57:10 +02:00
parent 6d21288d31
commit f5b71a3a5f
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 30 additions and 0 deletions

View File

@ -471,6 +471,16 @@ class FunctionCallReturnTypeFetcher
}
return $call_map_return_type;
case 'mb_strtolower':
if (count($call_args) < 2) {
return Type::getLowercaseString();
} else {
$second_arg_type = $statements_analyzer->node_data->getType($call_args[1]->value);
if ($second_arg_type && $second_arg_type->isNull()) {
return Type::getLowercaseString();
}
}
return Type::getString();
}
}

View File

@ -1676,6 +1676,26 @@ class FunctionCallTest extends TestCase
if (strpos(DateTime::class, $needle)) {}
}',
],
'mb_strtolowerProducesStringWithSecondArgument' => [
'<?php
$r = mb_strtolower("École", "BASE64");
',
'assertions' => [
'$r===' => 'string',
],
],
'mb_strtolowerProducesLowercaseStringWithNullOrAbsentEncoding' => [
'<?php
$a = mb_strtolower("AAA");
$b = mb_strtolower("AAA", null);
',
'assertions' => [
'$a===' => 'lowercase-string',
'$b===' => 'lowercase-string',
],
[],
'8.1',
],
];
}