Skip to content

Commit 8a95b77

Browse files
committed
mb_convert_encoding-can-return-false
1 parent 5bfe8f1 commit 8a95b77

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

resources/functionMap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6312,7 +6312,7 @@
63126312
'mb_check_encoding' => ['bool', 'var='=>'string|array<string>', 'encoding='=>'string'],
63136313
'mb_chr' => ['string|false', 'cp'=>'int', 'encoding='=>'string'],
63146314
'mb_convert_case' => ['string', 'sourcestring'=>'string', 'mode'=>'int', 'encoding='=>'string'],
6315-
'mb_convert_encoding' => ['string|array<int, string>|false', 'val'=>'string|array<int, string>', 'to_encoding'=>'string', 'from_encoding='=>'mixed'],
6315+
'mb_convert_encoding' => ['string|array<scalar|null|array<scalar|null>>|false', 'val'=>'string|array<scalar|null|array<scalar|null>>', 'to_encoding'=>'string', 'from_encoding='=>'mixed'],
63166316
'mb_convert_kana' => ['string', 'str'=>'string', 'option='=>'string', 'encoding='=>'string'],
63176317
'mb_convert_variables' => ['string|false', 'to_encoding'=>'string', 'from_encoding'=>'array|string', '&rw_vars'=>'string|array|object', '&...rw_vars='=>'string|array|object'],
63186318
'mb_decode_mimeheader' => ['string', 'string'=>'string'],

src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Type\ArrayType;
8+
use PHPStan\Type\Constant\ConstantBooleanType;
99
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
10-
use PHPStan\Type\IntegerType;
11-
use PHPStan\Type\StringType;
1210
use PHPStan\Type\Type;
11+
use PHPStan\Type\UnionType;
1312

1413
final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
1514
{
@@ -25,18 +24,16 @@ public function getTypeFromFunctionCall(
2524
Scope $scope,
2625
): ?Type
2726
{
28-
if (!isset($functionCall->getArgs()[0])) {
27+
$args = $functionCall->getArgs();
28+
if (!isset($args[0])) {
2929
return null;
3030
}
3131

32-
$argType = $scope->getType($functionCall->getArgs()[0]->value);
32+
$argType = $scope->getType($args[0]->value);
3333
$isString = $argType->isString();
3434
$isArray = $argType->isArray();
35-
$compare = $isString->compareTo($isArray);
36-
if ($compare === $isString) {
37-
return new StringType();
38-
} elseif ($compare === $isArray) {
39-
return new ArrayType(new IntegerType(), new StringType());
35+
if ($isString->yes() || $isArray->yes()) {
36+
return new UnionType([$argType, new ConstantBooleanType(false)]);
4037
}
4138

4239
return null;

tests/PHPStan/Analyser/nsrt/bug-3336.php

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PHPStan\Analyser\nsrt\mb_convert_encoding;
4+
5+
/**
6+
* @param array{foo: string, bar: int} $structuredArray
7+
* @param list<string> $stringList
8+
* @param list<int> $intList
9+
*/
10+
function test_mb_convert_encoding(
11+
mixed $mixed,
12+
string $string,
13+
array $mixedArray,
14+
array $structuredArray,
15+
array $stringList,
16+
array $intList,
17+
): void {
18+
\PHPStan\Testing\assertType('array<array<bool|float|int|string|null>|bool|float|int|string|null>|string|false', mb_convert_encoding($mixed, 'UTF-8'));
19+
\PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8'));
20+
\PHPStan\Testing\assertType('array|false', mb_convert_encoding($mixedArray, 'UTF-8'));
21+
\PHPStan\Testing\assertType('array{foo: string, bar: int}|false', mb_convert_encoding($structuredArray, 'UTF-8'));
22+
\PHPStan\Testing\assertType('list<string>|false', mb_convert_encoding($stringList, 'UTF-8'));
23+
\PHPStan\Testing\assertType('list<int>|false', mb_convert_encoding($intList, 'UTF-8'));
24+
};
25+

0 commit comments

Comments
 (0)