diff --git a/src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php b/src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php index f567eadfb6..f12efd095c 100644 --- a/src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php +++ b/src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php @@ -20,6 +20,8 @@ use PHPStan\Type\UnionType; use function count; use function str_contains; +use function strtolower; +use function trim; #[AutowiredService] final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension @@ -81,7 +83,10 @@ public function getTypeFromFunctionCall( $constantStrings = $fromEncodingArgType->getConstantStrings(); if (count($constantStrings) > 0) { foreach ($constantStrings as $constantString) { - if (str_contains($constantString->getValue(), ',')) { + if ( + str_contains($constantString->getValue(), ',') + || trim(strtolower($constantString->getValue())) === 'auto' + ) { $returnFalseIfCannotDetectEncoding = true; break; } diff --git a/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php7.php b/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php7.php index d1de6c7c24..2fb84826f9 100644 --- a/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php7.php +++ b/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php7.php @@ -29,4 +29,7 @@ function test_mb_convert_encoding( \PHPStan\Testing\assertType('list|false', mb_convert_encoding($intList, 'UTF-8')); \PHPStan\Testing\assertType('array{foo: string, bar: int, baz: string}|string|false', mb_convert_encoding($union, 'UTF-8')); \PHPStan\Testing\assertType('array|string|false', mb_convert_encoding($int, 'UTF-8')); + + \PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', 'auto')); + \PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', ' AUTO ')); }; diff --git a/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php8.php b/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php8.php index a4d4121e18..e96f8f0e1a 100644 --- a/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php8.php +++ b/tests/PHPStan/Analyser/nsrt/mb-convert-encoding-php8.php @@ -42,4 +42,7 @@ function test_mb_convert_encoding( \PHPStan\Testing\assertType('list', mb_convert_encoding($stringList, 'UTF-8', ['FOO'])); \PHPStan\Testing\assertType('list|false', mb_convert_encoding($stringList, 'UTF-8', ['FOO', 'BAR'])); \PHPStan\Testing\assertType('list', mb_convert_encoding($stringList, 'UTF-8', ['FOO,BAR'])); + + \PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', 'auto')); + \PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', ' AUTO ')); };