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

mb_internal_encoding() always returns string when called without an argument #7408

Closed
ThomasLandauer opened this issue Jan 16, 2022 · 4 comments
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm Help wanted internal stubs/callmap

Comments

@ThomasLandauer
Copy link
Contributor

mb_internal_encoding() returns string|bool in principle, but when called without an argument, it's always string: https://www.php.net/manual/en/function.mb-internal-encoding.php

Example:
https://psalm.dev/r/32218305f6

PHPStan does take this into account, and is not reporting anything.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/32218305f6
<?php
function foo(string $encoding): void
{
    echo $encoding;
}
foo(mb_internal_encoding());
Psalm output (using commit 9190294):

ERROR: InvalidScalarArgument - 6:5 - Argument 1 of foo expects string, bool|string provided

@orklah
Copy link
Collaborator

orklah commented Jan 16, 2022

This can be handled by adding a new stub for this function, kinda like for strlen: https://github.com/vimeo/psalm/blob/4.x/stubs/CoreGenericFunctions.phpstub#L742

With a conditional return, you'll be able to check that if the param is an empty-string, the return will be a string.

Could you check if the function ever return true as well?

@orklah orklah added bug easy problems Issues that can be fixed without background knowledge of Psalm Help wanted internal stubs/callmap labels Jan 16, 2022
@derrabus
Copy link

Could you check if the function ever return true as well?

It does return true if you call it with a valid $encoding parameter. On PHP 8 though, it will never return false because calling the function with an invalid encoding will trigger a ValueError. Also, the behavior for null has changed in PHP 8.

The rules for the return type are:

$encoding PHP 7 PHP 8
omitted string string
valid encoding string true true
invalid encoding string false+ E_WARNING triggered ValueError is thrown
null false + E_WARNING triggered string
non-stringable parameter null + E_WARNING triggered TypeError is thrown

@orklah
Copy link
Collaborator

orklah commented Jan 19, 2022

Thanks @derrabus, looking at your chart, I thought better to create a return type provider instead of a stub with a big conditional return.

This will ship with Psalm 5 soon

@orklah orklah closed this as completed Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm Help wanted internal stubs/callmap
Projects
None yet
Development

No branches or pull requests

3 participants