Skip to content

Improve mb_convert_encoding return type #4010

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

Open
wants to merge 3 commits into
base: 1.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions src/Type/Php/MbConvertEncodingFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand All @@ -17,10 +18,15 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function count;
use function str_contains;

final class MbConvertEncodingFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(private PhpVersion $phpVersion)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'mb_convert_encoding';
Expand Down Expand Up @@ -49,6 +55,45 @@ public function getTypeFromFunctionCall(
return null;
}

if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
if (!isset($functionCall->getArgs()[2])) {
return $result;
}
$fromEncodingArgType = $scope->getType($functionCall->getArgs()[2]->value);

$mayNotDetectEncoding = false;
if (!$fromEncodingArgType->isArray()->no()) {
$constantArrays = $fromEncodingArgType->getConstantArrays();
if (count($constantArrays) > 0) {
foreach ($constantArrays as $constantArray) {
if (count($constantArray->getValueTypes()) > 1) {
$mayNotDetectEncoding = true;
break;
}
}
} else {
$mayNotDetectEncoding = true;
}
}
if (!$mayNotDetectEncoding && !$fromEncodingArgType->isString()->no()) {
$constantStrings = $fromEncodingArgType->getConstantStrings();
if (count($constantStrings) > 0) {
foreach ($constantStrings as $constantString) {
if (str_contains($constantString->getValue(), ',')) {
$mayNotDetectEncoding = true;
break;
}
}
} else {
$mayNotDetectEncoding = true;
}
}

if (!$mayNotDetectEncoding) {
return $result;
}
}

return TypeCombinator::union($result, new ConstantBooleanType(false));
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/data/explode-php80.php';
}

if (PHP_VERSION_ID < 80000) {
yield __DIR__ . '/data/mb-convert-encoding-php7.php';
} else {
yield __DIR__ . '/data/mb-convert-encoding-php8.php';
}

if (PHP_VERSION_ID >= 80000) {
yield __DIR__ . '/../Reflection/data/unionTypes.php';
yield __DIR__ . '/../Reflection/data/mixedType.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MbConvertEncoding;
namespace MbConvertEncodingPHP7;

/**
* @param 'foo'|'bar' $constantString
Expand Down
45 changes: 45 additions & 0 deletions tests/PHPStan/Analyser/data/mb-convert-encoding-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MbConvertEncodingPHP8;

/**
* @param 'foo'|'bar' $constantString
* @param array{foo: string, bar: int, baz: 'foo'} $structuredArray
* @param list<string> $stringList
* @param list<int> $intList
* @param 'foo'|'bar'|array{foo: string, bar: int, baz: 'foo'}|bool $union
*/
function test_mb_convert_encoding(
mixed $mixed,
string $constantString,
string $string,
array $mixedArray,
array $structuredArray,
array $stringList,
array $intList,
string|array|bool $union,
int $int,
): void {
\PHPStan\Testing\assertType('array|string|false', mb_convert_encoding($mixed, 'UTF-8'));
\PHPStan\Testing\assertType('string', mb_convert_encoding($constantString, 'UTF-8'));
\PHPStan\Testing\assertType('string', mb_convert_encoding($string, 'UTF-8'));
\PHPStan\Testing\assertType('array', mb_convert_encoding($mixedArray, 'UTF-8'));
\PHPStan\Testing\assertType('array{foo: string, bar: int, baz: string}', mb_convert_encoding($structuredArray, 'UTF-8'));
\PHPStan\Testing\assertType('list<string>', mb_convert_encoding($stringList, 'UTF-8'));
\PHPStan\Testing\assertType('list<int>', 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', mb_convert_encoding($string, 'UTF-8', 'FOO'));
\PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', $string));
\PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', 'FOO,BAR'));
\PHPStan\Testing\assertType('string', mb_convert_encoding($string, 'UTF-8', ['FOO']));
\PHPStan\Testing\assertType('string|false', mb_convert_encoding($string, 'UTF-8', ['FOO', 'BAR']));
\PHPStan\Testing\assertType('string', mb_convert_encoding($string, 'UTF-8', ['FOO,BAR']));
\PHPStan\Testing\assertType('list<string>', mb_convert_encoding($stringList, 'UTF-8', 'FOO'));
\PHPStan\Testing\assertType('list<string>|false', mb_convert_encoding($stringList, 'UTF-8', $string));
\PHPStan\Testing\assertType('list<string>|false', mb_convert_encoding($stringList, 'UTF-8', 'FOO,BAR'));
\PHPStan\Testing\assertType('list<string>', mb_convert_encoding($stringList, 'UTF-8', ['FOO']));
\PHPStan\Testing\assertType('list<string>|false', mb_convert_encoding($stringList, 'UTF-8', ['FOO', 'BAR']));
\PHPStan\Testing\assertType('list<string>', mb_convert_encoding($stringList, 'UTF-8', ['FOO,BAR']));
};
Loading