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

More precise signatures for mb_* functions #717

Merged
merged 6 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions resources/functionMap.php
Expand Up @@ -6345,18 +6345,18 @@
'mb_split' => ['array|false', 'pattern'=>'string', 'string'=>'string', 'limit='=>'int'],
'mb_strcut' => ['string', 'str'=>'string', 'start'=>'int', 'length='=>'?int', 'encoding='=>'string'],
'mb_strimwidth' => ['string', 'str'=>'string', 'start'=>'int', 'width'=>'int', 'trimmarker='=>'string', 'encoding='=>'string'],
'mb_stripos' => ['int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_stripos' => ['0|positive-int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_stristr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'part='=>'bool', 'encoding='=>'string'],
'mb_strlen' => ['int|false', 'str'=>'string', 'encoding='=>'string'],
'mb_strpos' => ['int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strlen' => ['0|positive-int|false', 'str'=>'string', 'encoding='=>'string'],
'mb_strpos' => ['0|positive-int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strrchr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'part='=>'bool', 'encoding='=>'string'],
'mb_strrichr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'part='=>'bool', 'encoding='=>'string'],
'mb_strripos' => ['int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strrpos' => ['int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strripos' => ['0|positive-int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strrpos' => ['0|positive-int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strstr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'part='=>'bool', 'encoding='=>'string'],
'mb_strtolower' => ['string', 'str'=>'string', 'encoding='=>'string'],
'mb_strtoupper' => ['string', 'str'=>'string', 'encoding='=>'string'],
'mb_strwidth' => ['int', 'str'=>'string', 'encoding='=>'string'],
'mb_strwidth' => ['0|positive-int', 'str'=>'string', 'encoding='=>'string'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

'mb_substitute_character' => ['mixed', 'substchar='=>'mixed'],
'mb_substr' => ['string', 'str'=>'string', 'start'=>'int', 'length='=>'?int', 'encoding='=>'string'],
'mb_substr_count' => ['0|positive-int', 'haystack'=>'string', 'needle'=>'string', 'encoding='=>'string'],
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Php/MbFunctionsReturnTypeExtension.php
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -74,6 +75,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
$positionEncodingParam = $this->encodingPositionMap[$functionReflection->getName()];

// php8-stubs define a regular int return-type for mb_strlen. use a more precise type instead.
Copy link
Contributor Author

@staabm staabm Feb 1, 2022

Choose a reason for hiding this comment

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

it seems ParametersAcceptorSelector::selectSingle doesn't pick the return type from the functionMap but from the php8 stubs, where mb_strlen is defined with a int return type.

therefore we need this additional if-case

Copy link
Member

Choose a reason for hiding this comment

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

Can you try putting 0|positive-int return type in functionMap_php80delta.php and delete this condition? It might work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh that worked - thanks!

if ($functionReflection->getName() === 'mb_strlen') {
$returnType = IntegerRangeType::fromInterval(0, null);
}

if (count($functionCall->getArgs()) < $positionEncodingParam) {
return TypeCombinator::remove($returnType, new BooleanType());
}
Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Expand Up @@ -5302,27 +5302,27 @@ public function dataFunctions(): array
'$versionCompare8',
],
[
'int',
'int<0, max>',
'$mbStrlenWithoutEncoding',
],
[
'int',
'int<0, max>',
'$mbStrlenWithValidEncoding',
],
[
'int',
'int<0, max>',
'$mbStrlenWithValidEncodingAlias',
],
[
'false',
'$mbStrlenWithInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'int|false' : 'int',
PHP_VERSION_ID < 80000 ? 'int<0, max>|false' : 'int<0, max>',
'$mbStrlenWithValidAndInvalidEncoding',
],
[
PHP_VERSION_ID < 80000 ? 'int|false' : 'int',
PHP_VERSION_ID < 80000 ? 'int<0, max>|false' : 'int<0, max>',
'$mbStrlenWithUnknownEncoding',
],
[
Expand Down