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

Better typing for mb_convert_encoding #3749

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from

Conversation

jack-worman
Copy link
Contributor

Closes phpstan/phpstan#12309

Also, fixed types related to the array input and output; the output array has the exact same shape as the input array: https://3v4l.org/OrdDb

@jack-worman jack-worman force-pushed the mb_convert_encoding-can-return-false branch 2 times, most recently from e514bc3 to 6f121ae Compare December 21, 2024 18:58
@@ -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<scalar|null|array<scalar|null>>|false', 'val'=>'string|array<scalar|null|array<scalar|null>>', 'to_encoding'=>'string', 'from_encoding='=>'mixed'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to represent a recursive type?
Or is only going 3 layers deep good enough?

@jack-worman jack-worman force-pushed the mb_convert_encoding-can-return-false branch 3 times, most recently from 7004ae4 to 8a95b77 Compare December 29, 2024 16:33
@jack-worman jack-worman force-pushed the mb_convert_encoding-can-return-false branch from 8a95b77 to daee21d Compare December 31, 2024 21:45
} elseif ($compare === $isArray) {
return new ArrayType(new IntegerType(), new StringType());
if ($isString->yes() || $isArray->yes()) {
return new UnionType([$argType, new ConstantBooleanType(false)]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all versions, the mb_convert_encoding() function throws an error for invalid encodings, so it makes sense to use BenevolentUnionType instead of Union. https://3v4l.org/YuXmF

Suggested change
return new UnionType([$argType, new ConstantBooleanType(false)]);
return new BenevolentUnionType([$argType, new ConstantBooleanType(false)]);

Using a benevolent union, we can pass it to function calls expecting strings without any extra assertions, like this: https://phpstan.org/r/fbcfe268-adc6-48cd-9f35-c66b3117c269

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strict comparison using === between string and false will always evaluate to false.
2 participants