Skip to content

Commit 15dce0c

Browse files
authored
Modify functionMap and add __benevolent<> (#2986)
1 parent 4c167d7 commit 15dce0c

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

resources/functionMap.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@
933933
'CallbackFilterIterator::next' => ['void'],
934934
'CallbackFilterIterator::rewind' => ['void'],
935935
'CallbackFilterIterator::valid' => ['bool'],
936-
'ceil' => ['float|false', 'number'=>'float'],
936+
'ceil' => ['__benevolent<float|false>', 'number'=>'float'],
937937
'chdb::__construct' => ['void', 'pathname'=>'string'],
938938
'chdb::get' => ['string', 'key'=>'string'],
939939
'chdb_create' => ['bool', 'pathname'=>'string', 'data'=>'array'],
@@ -2991,7 +2991,7 @@
29912991
'finfo_set_flags' => ['bool', 'finfo'=>'resource', 'options'=>'int'],
29922992
'floatval' => ['float', 'var'=>'scalar|array|resource|null'],
29932993
'flock' => ['bool', 'fp'=>'resource', 'operation'=>'int', '&w_wouldblock='=>'int'],
2994-
'floor' => ['float|false', 'number'=>'float'],
2994+
'floor' => ['__benevolent<float|false>', 'number'=>'float'],
29952995
'flush' => ['void'],
29962996
'fmod' => ['float', 'x'=>'float', 'y'=>'float'],
29972997
'fnmatch' => ['bool', 'pattern'=>'string', 'filename'=>'string', 'flags='=>'int'],
@@ -3311,7 +3311,7 @@
33113311
'get_extension_funcs' => ['list<callable-string>|false', 'extension_name'=>'string'],
33123312
'get_headers' => ['array|false', 'url'=>'string', 'format='=>'int', 'context='=>'resource'],
33133313
'get_html_translation_table' => ['array', 'table='=>'int', 'flags='=>'int', 'encoding='=>'string'],
3314-
'get_include_path' => ['string|false'],
3314+
'get_include_path' => ['__benevolent<string|false>'],
33153315
'get_included_files' => ['list<string>'],
33163316
'get_loaded_extensions' => ['list<string>', 'zend_extensions='=>'bool'],
33173317
'get_magic_quotes_gpc' => ['false'],
@@ -6494,8 +6494,8 @@
64946494
'MemcachePool::set' => ['bool', 'key'=>'string', 'var'=>'mixed', 'flag='=>'int', 'expire='=>'int'],
64956495
'MemcachePool::setCompressThreshold' => ['bool', 'threshold'=>'int', 'min_savings='=>'float'],
64966496
'MemcachePool::setServerParams' => ['bool', 'host'=>'string', 'port='=>'int', 'timeout='=>'int', 'retry_interval='=>'int', 'status='=>'bool', 'failure_callback='=>'callable'],
6497-
'memory_get_peak_usage' => ['int', 'real_usage='=>'bool'],
6498-
'memory_get_usage' => ['int', 'real_usage='=>'bool'],
6497+
'memory_get_peak_usage' => ['positive-int', 'real_usage='=>'bool'],
6498+
'memory_get_usage' => ['positive-int', 'real_usage='=>'bool'],
64996499
'MessageFormatter::__construct' => ['void', 'locale'=>'string', 'pattern'=>'string'],
65006500
'MessageFormatter::create' => ['MessageFormatter', 'locale'=>'string', 'pattern'=>'string'],
65016501
'MessageFormatter::format' => ['false|string', 'args'=>'array'],
@@ -6525,7 +6525,7 @@
65256525
'ming_useconstants' => ['void', 'use'=>'int'],
65266526
'ming_useswfversion' => ['void', 'version'=>'int'],
65276527
'mkdir' => ['bool', 'pathname'=>'string', 'mode='=>'int', 'recursive='=>'bool', 'context='=>'resource'],
6528-
'mktime' => ['int|false', 'hour='=>'int', 'min='=>'int', 'sec='=>'int', 'mon='=>'int', 'day='=>'int', 'year='=>'int'],
6528+
'mktime' => ['__benevolent<int|false>', 'hour='=>'int', 'min='=>'int', 'sec='=>'int', 'mon='=>'int', 'day='=>'int', 'year='=>'int'],
65296529
'money_format' => ['string', 'format'=>'string', 'value'=>'float'],
65306530
'Mongo::__construct' => ['void', 'server='=>'string', 'options='=>'array', 'driver_options='=>'array'],
65316531
'Mongo::__get' => ['MongoDB', 'dbname'=>'string'],
@@ -8962,10 +8962,10 @@
89628962
'phdfs::tell' => ['int', 'path'=>'string'],
89638963
'phdfs::write' => ['bool', 'path'=>'string', 'buffer'=>'string', 'mode='=>'string'],
89648964
'php_check_syntax' => ['bool', 'filename'=>'string', 'error_message='=>'string'],
8965-
'php_ini_loaded_file' => ['string|false'],
8965+
'php_ini_loaded_file' => ['non-empty-string|false'],
89668966
'php_ini_scanned_files' => ['string|false'],
89678967
'php_logo_guid' => ['string'],
8968-
'php_sapi_name' => ['string|false'],
8968+
'php_sapi_name' => ['__benevolent<non-empty-string|false>'],
89698969
'php_strip_whitespace' => ['string', 'file_name'=>'string'],
89708970
'php_uname' => ['string', 'mode='=>'string'],
89718971
'php_user_filter::filter' => ['int', 'in'=>'resource', 'out'=>'resource', '&rw_consumed'=>'int', 'closing'=>'bool'],
@@ -10182,7 +10182,7 @@
1018210182
'rewind' => ['bool', 'fp'=>'resource'],
1018310183
'rewinddir' => ['null|false', 'dir_handle='=>'resource'],
1018410184
'rmdir' => ['bool', 'dirname'=>'string', 'context='=>'resource'],
10185-
'round' => ['float|false', 'number'=>'float', 'precision='=>'int', 'mode='=>'1|2|3|4'],
10185+
'round' => ['__benevolent<float|false>', 'number'=>'float', 'precision='=>'int', 'mode='=>'1|2|3|4'],
1018610186
'rpm_close' => ['bool', 'rpmr'=>'resource'],
1018710187
'rpm_get_tag' => ['mixed', 'rpmr'=>'resource', 'tagnum'=>'int'],
1018810188
'rpm_is_valid' => ['bool', 'filename'=>'string'],

resources/functionMap_php80delta.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'bcmod' => ['string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
2929
'bcpowmod' => ['string', 'base'=>'string', 'exponent'=>'string', 'modulus'=>'string', 'scale='=>'int'],
3030
'call_user_func_array' => ['mixed', 'function'=>'callable', 'parameters'=>'array<int|string,mixed>'],
31+
'ceil' => ['float', 'number'=>'float'],
3132
'com_load_typelib' => ['bool', 'typelib_name'=>'string', 'case_insensitive='=>'true'],
3233
'count_chars' => ['array<int,int>|string', 'input'=>'string', 'mode='=>'int'],
3334
'date_add' => ['DateTime', 'object'=>'DateTime', 'interval'=>'DateInterval'],
@@ -43,6 +44,7 @@
4344
'date_timezone_set' => ['DateTime', 'object'=>'DateTime', 'timezone'=>'DateTimeZone'],
4445
'explode' => ['list<string>', 'separator'=>'non-empty-string', 'str'=>'string', 'limit='=>'int'],
4546
'fdiv' => ['float', 'dividend'=>'float', 'divisor'=>'float'],
47+
'floor' => ['float', 'number'=>'float'],
4648
'forward_static_call_array' => ['mixed', 'function'=>'callable', 'parameters'=>'array<int|string,mixed>'],
4749
'get_debug_type' => ['string', 'var'=>'mixed'],
4850
'get_resource_id' => ['int', 'res'=>'resource'],
@@ -108,6 +110,7 @@
108110
'strrpos' => ['int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int'],
109111
'strstr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'before_needle='=>'bool'],
110112
'substr' => ['string', 'string'=>'string', 'start'=>'int', 'length='=>'int'],
113+
'round' => ['float', 'number'=>'float', 'precision='=>'int', 'mode='=>'1|2|3|4'],
111114
'version_compare' => ['int|bool', 'version1'=>'string', 'version2'=>'string', 'operator='=>'string'],
112115
'xml_parser_create' => ['XMLParser', 'encoding='=>'string'],
113116
'xml_parser_create_ns' => ['XMLParser', 'encoding='=>'string', 'sep='=>'string'],
@@ -162,6 +165,7 @@
162165
'bcdiv' => ['?string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
163166
'bcmod' => ['?string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
164167
'bcpowmod' => ['?string', 'base'=>'string', 'exponent'=>'string', 'modulus'=>'string', 'scale='=>'int'],
168+
'ceil' => ['__benevolent<float|false>', 'number'=>'float'],
165169
'convert_cyr_string' => ['string', 'str'=>'string', 'from'=>'string', 'to'=>'string'],
166170
'com_load_typelib' => ['bool', 'typelib_name'=>'string', 'case_insensitive='=>'bool'],
167171
'count_chars' => ['array<int,int>|false|string', 'input'=>'string', 'mode='=>'int'],
@@ -179,6 +183,7 @@
179183
'each' => ['array{0:int|string,key:int|string,1:mixed,value:mixed}', '&r_arr'=>'array'],
180184
'ezmlm_hash' => ['int', 'addr'=>'string'],
181185
'fgetss' => ['string|false', 'fp'=>'resource', 'length='=>'0|positive-int', 'allowable_tags='=>'string'],
186+
'floor' => ['__benevolent<float|false>', 'number'=>'float'],
182187
'get_magic_quotes_gpc' => ['false'],
183188
'gmdate' => ['string|false', 'format'=>'string', 'timestamp='=>'int'],
184189
'gmmktime' => ['int|false', 'hour='=>'int', 'minute='=>'int', 'second='=>'int', 'month='=>'int', 'day='=>'int', 'year='=>'int'],
@@ -231,6 +236,7 @@
231236
'proc_get_status' => ['array{command: string, pid: int, running: bool, signaled: bool, stopped: bool, exitcode: int, termsig: int, stopsig: int}|false', 'process'=>'resource'],
232237
'read_exif_data' => ['array', 'filename'=>'string', 'sections_needed='=>'string', 'sub_arrays='=>'bool', 'read_thumbnail='=>'bool'],
233238
'restore_include_path' => ['void'],
239+
'round' => ['__benevolent<float|false>', 'number'=>'float', 'precision='=>'int', 'mode='=>'1|2|3|4'],
234240
'socket_select' => ['int|false', '&rw_read_fds'=>'resource[]|null', '&rw_write_fds'=>'resource[]|null', '&rw_except_fds'=>'resource[]|null', 'tv_sec'=>'int|null', 'tv_usec='=>'int|null'],
235241
'sodium_crypto_aead_chacha20poly1305_ietf_decrypt' => ['?string|?false', 'confidential_message'=>'string', 'public_message'=>'string', 'nonce'=>'string', 'key'=>'string'],
236242
'SplFileObject::fgetss' => ['string|false', 'allowable_tags='=>'string'],

src/Type/Php/RoundFunctionReturnTypeExtension.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Php\PhpVersion;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\Type\BenevolentUnionType;
109
use PHPStan\Type\Constant\ConstantBooleanType;
1110
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1211
use PHPStan\Type\FloatType;
@@ -39,21 +38,18 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
3938
);
4039
}
4140

42-
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
41+
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
4342
{
43+
// PHP 7 can return either a float or false.
44+
// PHP 8 can either return a float or fatal.
45+
$defaultReturnType = null;
46+
4447
if ($this->phpVersion->hasStricterRoundFunctions()) {
4548
// PHP 8 fatals with a missing parameter.
4649
$noArgsReturnType = new NeverType(true);
47-
// PHP 8 can either return a float or fatal.
48-
$defaultReturnType = new FloatType();
4950
} else {
5051
// PHP 7 returns null with a missing parameter.
5152
$noArgsReturnType = new NullType();
52-
// PHP 7 can return either a float or false.
53-
$defaultReturnType = new BenevolentUnionType([
54-
new FloatType(),
55-
new ConstantBooleanType(false),
56-
]);
5753
}
5854

5955
if (count($functionCall->getArgs()) < 1) {

0 commit comments

Comments
 (0)