Skip to content

Return type for Safe\mb_covert_encoding() is not correctly inferred from parameters #68

@acabal

Description

@acabal

The mb_convert_encoding() function accepts either a string or an array as parameter 1. Its return type is the same type as parameter 1's type.

PHPStan understands this with base PHP. But when using Safe, the return type of mb_convert_encoding() is always string|array regardless of the type of parameter 1.

Consider:

// `mb_convert_encoding()` is passed a string and will therefore return a string (and not an array)
$foo = \mb_convert_encoding('string', 'utf-8');

// Got expected PHPStan output, because `stripos()` expects a string and got a string.
$baz = stripos($foo, '');

// Now using the Safe version...
$bar = \Safe\mb_convert_encoding('string', 'utf-8');

// Failure: PHPStan outputs `Parameter #1 $haystack of function stripos expects string, array|string given.`
// The Safe version returns type string|array even though the input was a string
$baz = stripos($bar, '');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions