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

Modify functionMap and add __benevolent<> #2986

Merged
merged 3 commits into from Mar 22, 2024

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Mar 22, 2024

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.

@phpstan-bot
Copy link
Collaborator

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.

@zonuexe zonuexe changed the base branch from 1.11.x to 1.10.x March 22, 2024 19:18
@@ -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'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as RoundFunctionReturnTypeExtension.

$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>'],
Copy link
Contributor Author

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.

https://github.com/php/php-src/blob/df017cd0ef9e89d97b64dcf01efc5395fab30f6c/ext/standard/basic_functions.c#L2084-L2099

'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'],
Copy link
Contributor Author

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.

https://3v4l.org/ZTQKN

@@ -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'],
Copy link
Contributor Author

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.

https://github.com/php/php-src/blob/df017cd0ef9e89d97b64dcf01efc5395fab30f6c/ext/date/php_date.c#L1209-L1213

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.

https://3v4l.org/dnSu2

$defaultReturnType = new BenevolentUnionType([
new FloatType(),
new ConstantBooleanType(false),
]);
Copy link
Contributor Author

@zonuexe zonuexe Mar 22, 2024

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.

@ondrejmirtes
Copy link
Member

Thank you, changes make sense 😊

@ondrejmirtes ondrejmirtes merged commit 15dce0c into phpstan:1.10.x Mar 22, 2024
436 of 441 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants