Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better typing for mb_convert_encoding #3749

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6312,7 +6312,7 @@
'mb_check_encoding' => ['bool', 'var='=>'string|array<string>', 'encoding='=>'string'],
'mb_chr' => ['string|false', 'cp'=>'int', 'encoding='=>'string'],
'mb_convert_case' => ['string', 'sourcestring'=>'string', 'mode'=>'int', 'encoding='=>'string'],
'mb_convert_encoding' => ['string|array<int, string>|false', 'val'=>'string|array<int, string>', 'to_encoding'=>'string', 'from_encoding='=>'mixed'],
'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'],
'mb_convert_kana' => ['string', 'str'=>'string', 'option='=>'string', 'encoding='=>'string'],
'mb_convert_variables' => ['string|false', 'to_encoding'=>'string', 'from_encoding'=>'array|string', '&rw_vars'=>'string|array|object', '&...rw_vars='=>'string|array|object'],
'mb_decode_mimeheader' => ['string', 'string'=>'string'],
Expand Down
19 changes: 7 additions & 12 deletions src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
Expand All @@ -25,18 +24,14 @@ public function getTypeFromFunctionCall(
Scope $scope,
): ?Type
{
if (!isset($functionCall->getArgs()[0])) {
$args = $functionCall->getArgs();
if (!isset($args[0])) {
return null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$isString = $argType->isString();
$isArray = $argType->isArray();
$compare = $isString->compareTo($isArray);
if ($compare === $isString) {
return new StringType();
} elseif ($compare === $isArray) {
return new ArrayType(new IntegerType(), new StringType());
$argType = $scope->getType($args[0]->value);
if ($argType->isString()->yes() || $argType->isArray()->yes()) {
return new BenevolentUnionType([$argType, new ConstantBooleanType(false)]);
}

return null;
Expand Down
10 changes: 0 additions & 10 deletions tests/PHPStan/Analyser/nsrt/bug-3336.php

This file was deleted.

24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/mb_convert_encoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PHPStan\Analyser\nsrt\mb_convert_encoding;

/**
* @param array{foo: string, bar: int} $structuredArray
* @param list<string> $stringList
* @param list<int> $intList
*/
function test_mb_convert_encoding(
mixed $mixed,
string $string,
array $mixedArray,
array $structuredArray,
array $stringList,
array $intList,
): void {
\PHPStan\Testing\assertType('(array<array<bool|float|int|string|null>|bool|float|int|string|null>|string|false)', mb_convert_encoding($mixed, 'UTF-8'));
\PHPStan\Testing\assertType('(string|false)', mb_convert_encoding($string, 'UTF-8'));
\PHPStan\Testing\assertType('(array|false)', mb_convert_encoding($mixedArray, 'UTF-8'));
\PHPStan\Testing\assertType('(array{foo: string, bar: int}|false)', mb_convert_encoding($structuredArray, 'UTF-8'));
\PHPStan\Testing\assertType('(list<string>|false)', mb_convert_encoding($stringList, 'UTF-8'));
\PHPStan\Testing\assertType('(list<int>|false)', mb_convert_encoding($intList, 'UTF-8'));
};
Loading