Skip to content

Commit

Permalink
mb_convert_encoding-can-return-false
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-worman committed Dec 21, 2024
1 parent 4d2883b commit e514bc3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
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' => ['string|array|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
17 changes: 7 additions & 10 deletions src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand All @@ -25,18 +24,16 @@ 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);
$argType = $scope->getType($args[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());
if ($isString->yes() || $isArray->yes()) {
return new UnionType([$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.

25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/nsrt/mb_convert_encoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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|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'));
};

0 comments on commit e514bc3

Please sign in to comment.