-
Notifications
You must be signed in to change notification settings - Fork 478
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
Modify functionMap and add __benevolent<> #2986
Modify functionMap and add __benevolent<> #2986
Conversation
You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x. |
@@ -2991,7 +2991,7 @@ | |||
'finfo_set_flags' => ['bool', 'finfo'=>'resource', 'options'=>'int'], | |||
'floatval' => ['float', 'var'=>'scalar|array|resource|null'], | |||
'flock' => ['bool', 'fp'=>'resource', 'operation'=>'int', '&w_wouldblock='=>'int'], | |||
'floor' => ['float|false', 'number'=>'float'], | |||
'floor' => ['__benevolent<float|false>', 'number'=>'float'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as RoundFunctionReturnTypeExtension
.
phpstan-src/src/Type/Php/RoundFunctionReturnTypeExtension.php
Lines 48 to 56 in f51d90c
$defaultReturnType = new FloatType(); | |
} else { | |
// PHP 7 returns null with a missing parameter. | |
$noArgsReturnType = new NullType(); | |
// PHP 7 can return either a float or false. | |
$defaultReturnType = new BenevolentUnionType([ | |
new FloatType(), | |
new ConstantBooleanType(false), | |
]); |
@@ -3311,7 +3311,7 @@ | |||
'get_extension_funcs' => ['list<callable-string>|false', 'extension_name'=>'string'], | |||
'get_headers' => ['array|false', 'url'=>'string', 'format='=>'int', 'context='=>'resource'], | |||
'get_html_translation_table' => ['array', 'table='=>'int', 'flags='=>'int', 'encoding='=>'string'], | |||
'get_include_path' => ['string|false'], | |||
'get_include_path' => ['__benevolent<string|false>'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the situation where the ini include_path
entry does not exist is an abnormal situation, it seems that __benevolent<>
can be added.
'memory_get_peak_usage' => ['int', 'real_usage='=>'bool'], | ||
'memory_get_usage' => ['int', 'real_usage='=>'bool'], | ||
'memory_get_peak_usage' => ['positive-int', 'real_usage='=>'bool'], | ||
'memory_get_usage' => ['positive-int', 'real_usage='=>'bool'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions return only positive values.
@@ -6525,7 +6525,7 @@ | |||
'ming_useconstants' => ['void', 'use'=>'int'], | |||
'ming_useswfversion' => ['void', 'version'=>'int'], | |||
'mkdir' => ['bool', 'pathname'=>'string', 'mode='=>'int', 'recursive='=>'bool', 'context='=>'resource'], | |||
'mktime' => ['int|false', 'hour='=>'int', 'min='=>'int', 'sec='=>'int', 'mon='=>'int', 'day='=>'int', 'year='=>'int'], | |||
'mktime' => ['__benevolent<int|false>', 'hour='=>'int', 'min='=>'int', 'sec='=>'int', 'mon='=>'int', 'day='=>'int', 'year='=>'int'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function returns false only when it receives a value outside the integer range.
PHP Manual says:
mktime()
returns the Unix timestamp of the arguments given, or false if the timestamp doesn't fit in a PHP integer.
It seems that even if PHP_INT_MAX
is passed for all parameters, this value will never be exceeded.
$defaultReturnType = new BenevolentUnionType([ | ||
new FloatType(), | ||
new ConstantBooleanType(false), | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extension change is unnecessary, but it removes the duplicate definition of the type with functionMap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just stumbled over
phpstan-src/src/Type/Php/StrtotimeFunctionReturnTypeExtension.php
Lines 39 to 41 in cbdc933
if ($argType instanceof MixedType) { | |
return TypeUtils::toBenevolentUnion($defaultReturnType); | |
} |
Thank you, changes make sense 😊 |
Add
__benevolent<>
to functions whose documentation says "return false", which return false when passed a parameter of the wrong type, or which do not return false upon normal use.Modify the functionMap for other functions, including those already covered by DynamicFunctionReturnTypeExtension.