Skip to content

Commit 50cf201

Browse files
committed
mb_convert_encoding-can-return-false
1 parent 1f7fe1c commit 50cf201

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

Diff for: 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' => ['__benevolent<string|array<scalar|null|array<scalar|null>>|false>', 'val'=>'string|array<scalar|null|array<scalar|null>>', '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'],

Diff for: src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Type\BenevolentUnionType;
98
use PHPStan\Type\Constant\ConstantBooleanType;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\Type;
11+
use PHPStan\Type\UnionType;
1212

1313
final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
1414
{
@@ -31,7 +31,7 @@ public function getTypeFromFunctionCall(
3131

3232
$argType = $scope->getType($args[0]->value);
3333
if ($argType->isString()->yes() || $argType->isArray()->yes()) {
34-
return new BenevolentUnionType([$argType, new ConstantBooleanType(false)]);
34+
return new UnionType([$argType, new ConstantBooleanType(false)]);
3535
}
3636

3737
return null;

Diff for: tests/PHPStan/Analyser/nsrt/bug-3336.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Bug3336;
4+
5+
function (array $arr, string $str, $mixed): void {
6+
\PHPStan\Testing\assertType('array|false', mb_convert_encoding($arr));
7+
\PHPStan\Testing\assertType('string|false', mb_convert_encoding($str));
8+
\PHPStan\Testing\assertType('array<array<bool|float|int|string|null>|bool|float|int|string|null>|string|false', mb_convert_encoding($mixed));
9+
\PHPStan\Testing\assertType('array<array<bool|float|int|string|null>|bool|float|int|string|null>|string|false', mb_convert_encoding());
10+
};

Diff for: tests/PHPStan/Analyser/nsrt/mb_convert_encoding.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function test_mb_convert_encoding(
1515
array $stringList,
1616
array $intList,
1717
): 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'));
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'));
2424
};

0 commit comments

Comments
 (0)