Skip to content

Commit 670d04d

Browse files
committed
Allow referencing StringAlwaysAcceptingObjectWithToStringType in functionMap
1 parent a04e0be commit 670d04d

File tree

3 files changed

+11
-50
lines changed

3 files changed

+11
-50
lines changed

resources/functionMap.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,8 +2999,8 @@
29992999
'forward_static_call_array' => ['mixed', 'function'=>'callable', 'parameters'=>'array<int,mixed>'],
30003000
'fpassthru' => ['0|positive-int|false', 'fp'=>'resource'],
30013001
'fpm_get_status' => ['array{pool: string, process-manager: \'dynamic\'|\'ondemand\'|\'static\', start-time: int<0, max>, start-since: int<0, max>, accepted-conn: int<0, max>, listen-queue: int<0, max>, max-listen-queue: int<0, max>, listen-queue-len: int<0, max>, idle-processes: int<0, max>, active-processes: int<1, max>, total-processes: int<1, max>, max-active-processes: int<1, max>, max-children-reached: 0|1, slow-requests: int<0, max>, procs: array<int, array{pid: int<2, max>, state: \'Idle\'|\'Running\', start-time: int<0, max>, start-since: int<0, max>, requests: int<0, max>, request-duration: int<0, max>, request-method: string, request-uri: string, query-string: string, request-length: int<0, max>, user: string, script: string, last-request-cpu: float, last-request-memory: int<0, max>}>}|false'],
3002-
'fprintf' => ['int', 'stream'=>'resource', 'format'=>'string', '...values='=>'string|int|float'],
3003-
'fputcsv' => ['0|positive-int|false', 'fp'=>'resource', 'fields'=>'array', 'delimiter='=>'string', 'enclosure='=>'string', 'escape_char='=>'string'],
3002+
'fprintf' => ['int', 'stream'=>'resource', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
3003+
'fputcsv' => ['0|positive-int|false', 'fp'=>'resource', 'fields'=>'array<int|string, __stringAndStringable|int|float|null|bool>', 'delimiter='=>'string', 'enclosure='=>'string', 'escape_char='=>'string'],
30043004
'fputs' => ['0|positive-int|false', 'fp'=>'resource', 'str'=>'string', 'length='=>'0|positive-int'],
30053005
'fread' => ['string|false', 'fp'=>'resource', 'length'=>'0|positive-int'],
30063006
'frenchtojd' => ['int', 'month'=>'int', 'day'=>'int', 'year'=>'int'],
@@ -8848,7 +8848,7 @@
88488848
'preg_split' => ['list<string>|false', 'pattern'=>'string', 'subject'=>'string', 'limit='=>'?int', 'flags='=>'int'],
88498849
'prev' => ['mixed', '&rw_array_arg'=>'array|object'],
88508850
'print_r' => ['string|true', 'var'=>'mixed', 'return='=>'bool'],
8851-
'printf' => ['int', 'format'=>'string', '...values='=>'string|int|float'],
8851+
'printf' => ['int', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
88528852
'proc_close' => ['int', 'process'=>'resource'],
88538853
'proc_get_status' => ['array{command: string, pid: int, running: bool, signaled: bool, stopped: bool, exitcode: int, termsig: int, stopsig: int}|false', 'process'=>'resource'],
88548854
'proc_nice' => ['bool', 'priority'=>'int'],
@@ -11406,7 +11406,7 @@
1140611406
'Spoofchecker::setAllowedLocales' => ['void', 'locale_list'=>'string'],
1140711407
'Spoofchecker::setChecks' => ['void', 'checks'=>'long'],
1140811408
'Spoofchecker::setRestrictionLevel' => ['void', 'restriction_level'=>'int'],
11409-
'sprintf' => ['string', 'format'=>'string', '...values='=>'string|int|float|bool'],
11409+
'sprintf' => ['string', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
1141011410
'sql_regcase' => ['string', 'string'=>'string'],
1141111411
'SQLite3::__construct' => ['void', 'filename'=>'string', 'flags='=>'int', 'encryption_key='=>'string|null'],
1141211412
'SQLite3::busyTimeout' => ['bool', 'msecs'=>'int'],
@@ -12791,7 +12791,7 @@
1279112791
'VarnishStat::getSnapshot' => ['array'],
1279212792
'version_compare' => ['int', 'version1'=>'string', 'version2'=>'string'],
1279312793
'version_compare\'1' => ['bool', 'version1'=>'string', 'version2'=>'string', 'operator'=>'string'],
12794-
'vfprintf' => ['int', 'stream'=>'resource', 'format'=>'string', 'args'=>'array'],
12794+
'vfprintf' => ['int', 'stream'=>'resource', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
1279512795
'virtual' => ['bool', 'uri'=>'string'],
1279612796
'Volatile::__construct' => ['void'],
1279712797
'Volatile::chunk' => ['array', 'size'=>'int', 'preserve'=>'bool'],
@@ -12828,8 +12828,8 @@
1282812828
'vpopmail_error' => ['string'],
1282912829
'vpopmail_passwd' => ['bool', 'user'=>'string', 'domain'=>'string', 'password'=>'string', 'apop='=>'bool'],
1283012830
'vpopmail_set_user_quota' => ['bool', 'user'=>'string', 'domain'=>'string', 'quota'=>'string'],
12831-
'vprintf' => ['int', 'format'=>'string', 'args'=>'array'],
12832-
'vsprintf' => ['string', 'format'=>'string', 'args'=>'array'],
12831+
'vprintf' => ['int', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
12832+
'vsprintf' => ['string', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
1283312833
'w32api_deftype' => ['bool', 'typename'=>'string', 'member1_type'=>'string', 'member1_name'=>'string', '...args='=>'string'],
1283412834
'w32api_init_dtype' => ['resource', 'typename'=>'string', 'value'=>'', '...args='=>''],
1283512835
'w32api_invoke_function' => ['', 'funcname'=>'string', 'argument'=>'', '...args='=>''],

src/PhpDoc/TypeNodeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
use PHPStan\Type\ResourceType;
8383
use PHPStan\Type\StaticType;
8484
use PHPStan\Type\StaticTypeFactory;
85+
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
8586
use PHPStan\Type\StringType;
8687
use PHPStan\Type\ThisType;
8788
use PHPStan\Type\Type;
@@ -397,6 +398,8 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
397398
}
398399

399400
return StaticTypeFactory::falsey();
401+
case '__stringandstringable':
402+
return new StringAlwaysAcceptingObjectWithToStringType();
400403
}
401404

402405
if ($nameScope->getClassName() !== null) {

src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@
1313
use PHPStan\Reflection\Native\NativeFunctionReflection;
1414
use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection;
1515
use PHPStan\TrinaryLogic;
16-
use PHPStan\Type\ArrayType;
17-
use PHPStan\Type\BooleanType;
1816
use PHPStan\Type\FileTypeMapper;
19-
use PHPStan\Type\FloatType;
2017
use PHPStan\Type\Generic\TemplateTypeMap;
21-
use PHPStan\Type\IntegerType;
2218
use PHPStan\Type\MixedType;
23-
use PHPStan\Type\NullType;
24-
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
25-
use PHPStan\Type\StringType;
2619
use PHPStan\Type\Type;
2720
use PHPStan\Type\TypehintHelper;
28-
use PHPStan\Type\UnionType;
2921
use function array_key_exists;
3022
use function array_map;
3123
use function strtolower;
@@ -98,7 +90,7 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
9890
$variants[] = new FunctionVariantWithPhpDocs(
9991
TemplateTypeMap::createEmpty(),
10092
null,
101-
array_map(static function (ParameterSignature $parameterSignature) use ($lowerCasedFunctionName, $phpDoc): NativeParameterWithPhpDocsReflection {
93+
array_map(static function (ParameterSignature $parameterSignature) use ($phpDoc): NativeParameterWithPhpDocsReflection {
10294
$type = $parameterSignature->getType();
10395

10496
$phpDocType = null;
@@ -108,40 +100,6 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
108100
$phpDocType = $phpDocParam->getType();
109101
}
110102
}
111-
if (
112-
$parameterSignature->getName() === 'values'
113-
&& (
114-
$lowerCasedFunctionName === 'printf'
115-
|| $lowerCasedFunctionName === 'sprintf'
116-
)
117-
) {
118-
$type = new UnionType([
119-
new StringAlwaysAcceptingObjectWithToStringType(),
120-
new IntegerType(),
121-
new FloatType(),
122-
new NullType(),
123-
new BooleanType(),
124-
]);
125-
}
126-
127-
if (
128-
$parameterSignature->getName() === 'fields'
129-
&& $lowerCasedFunctionName === 'fputcsv'
130-
) {
131-
$type = new ArrayType(
132-
new UnionType([
133-
new StringType(),
134-
new IntegerType(),
135-
]),
136-
new UnionType([
137-
new StringAlwaysAcceptingObjectWithToStringType(),
138-
new IntegerType(),
139-
new FloatType(),
140-
new NullType(),
141-
new BooleanType(),
142-
]),
143-
);
144-
}
145103

146104
return new NativeParameterWithPhpDocsReflection(
147105
$parameterSignature->getName(),

0 commit comments

Comments
 (0)