Skip to content

Commit

Permalink
Allow referencing StringAlwaysAcceptingObjectWithToStringType in func…
Browse files Browse the repository at this point in the history
…tionMap
  • Loading branch information
ondrejmirtes committed May 9, 2023
1 parent a04e0be commit 670d04d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 50 deletions.
14 changes: 7 additions & 7 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2999,8 +2999,8 @@
'forward_static_call_array' => ['mixed', 'function'=>'callable', 'parameters'=>'array<int,mixed>'],
'fpassthru' => ['0|positive-int|false', 'fp'=>'resource'],
'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'],
'fprintf' => ['int', 'stream'=>'resource', 'format'=>'string', '...values='=>'string|int|float'],
'fputcsv' => ['0|positive-int|false', 'fp'=>'resource', 'fields'=>'array', 'delimiter='=>'string', 'enclosure='=>'string', 'escape_char='=>'string'],
'fprintf' => ['int', 'stream'=>'resource', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
'fputcsv' => ['0|positive-int|false', 'fp'=>'resource', 'fields'=>'array<int|string, __stringAndStringable|int|float|null|bool>', 'delimiter='=>'string', 'enclosure='=>'string', 'escape_char='=>'string'],
'fputs' => ['0|positive-int|false', 'fp'=>'resource', 'str'=>'string', 'length='=>'0|positive-int'],
'fread' => ['string|false', 'fp'=>'resource', 'length'=>'0|positive-int'],
'frenchtojd' => ['int', 'month'=>'int', 'day'=>'int', 'year'=>'int'],
Expand Down Expand Up @@ -8848,7 +8848,7 @@
'preg_split' => ['list<string>|false', 'pattern'=>'string', 'subject'=>'string', 'limit='=>'?int', 'flags='=>'int'],
'prev' => ['mixed', '&rw_array_arg'=>'array|object'],
'print_r' => ['string|true', 'var'=>'mixed', 'return='=>'bool'],
'printf' => ['int', 'format'=>'string', '...values='=>'string|int|float'],
'printf' => ['int', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
'proc_close' => ['int', 'process'=>'resource'],
'proc_get_status' => ['array{command: string, pid: int, running: bool, signaled: bool, stopped: bool, exitcode: int, termsig: int, stopsig: int}|false', 'process'=>'resource'],
'proc_nice' => ['bool', 'priority'=>'int'],
Expand Down Expand Up @@ -11406,7 +11406,7 @@
'Spoofchecker::setAllowedLocales' => ['void', 'locale_list'=>'string'],
'Spoofchecker::setChecks' => ['void', 'checks'=>'long'],
'Spoofchecker::setRestrictionLevel' => ['void', 'restriction_level'=>'int'],
'sprintf' => ['string', 'format'=>'string', '...values='=>'string|int|float|bool'],
'sprintf' => ['string', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'],
'sql_regcase' => ['string', 'string'=>'string'],
'SQLite3::__construct' => ['void', 'filename'=>'string', 'flags='=>'int', 'encryption_key='=>'string|null'],
'SQLite3::busyTimeout' => ['bool', 'msecs'=>'int'],
Expand Down Expand Up @@ -12791,7 +12791,7 @@
'VarnishStat::getSnapshot' => ['array'],
'version_compare' => ['int', 'version1'=>'string', 'version2'=>'string'],
'version_compare\'1' => ['bool', 'version1'=>'string', 'version2'=>'string', 'operator'=>'string'],
'vfprintf' => ['int', 'stream'=>'resource', 'format'=>'string', 'args'=>'array'],
'vfprintf' => ['int', 'stream'=>'resource', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
'virtual' => ['bool', 'uri'=>'string'],
'Volatile::__construct' => ['void'],
'Volatile::chunk' => ['array', 'size'=>'int', 'preserve'=>'bool'],
Expand Down Expand Up @@ -12828,8 +12828,8 @@
'vpopmail_error' => ['string'],
'vpopmail_passwd' => ['bool', 'user'=>'string', 'domain'=>'string', 'password'=>'string', 'apop='=>'bool'],
'vpopmail_set_user_quota' => ['bool', 'user'=>'string', 'domain'=>'string', 'quota'=>'string'],
'vprintf' => ['int', 'format'=>'string', 'args'=>'array'],
'vsprintf' => ['string', 'format'=>'string', 'args'=>'array'],
'vprintf' => ['int', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
'vsprintf' => ['string', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'],
'w32api_deftype' => ['bool', 'typename'=>'string', 'member1_type'=>'string', 'member1_name'=>'string', '...args='=>'string'],
'w32api_init_dtype' => ['resource', 'typename'=>'string', 'value'=>'', '...args='=>''],
'w32api_invoke_function' => ['', 'funcname'=>'string', 'argument'=>'', '...args='=>''],
Expand Down
3 changes: 3 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
use PHPStan\Type\ResourceType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -397,6 +398,8 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
}

return StaticTypeFactory::falsey();
case '__stringandstringable':
return new StringAlwaysAcceptingObjectWithToStringType();
}

if ($nameScope->getClassName() !== null) {
Expand Down
44 changes: 1 addition & 43 deletions src/Reflection/SignatureMap/NativeFunctionReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,11 @@
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringAlwaysAcceptingObjectWithToStringType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use PHPStan\Type\UnionType;
use function array_key_exists;
use function array_map;
use function strtolower;
Expand Down Expand Up @@ -98,7 +90,7 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
$variants[] = new FunctionVariantWithPhpDocs(
TemplateTypeMap::createEmpty(),
null,
array_map(static function (ParameterSignature $parameterSignature) use ($lowerCasedFunctionName, $phpDoc): NativeParameterWithPhpDocsReflection {
array_map(static function (ParameterSignature $parameterSignature) use ($phpDoc): NativeParameterWithPhpDocsReflection {
$type = $parameterSignature->getType();

$phpDocType = null;
Expand All @@ -108,40 +100,6 @@ public function findFunctionReflection(string $functionName): ?NativeFunctionRef
$phpDocType = $phpDocParam->getType();
}
}
if (
$parameterSignature->getName() === 'values'
&& (
$lowerCasedFunctionName === 'printf'
|| $lowerCasedFunctionName === 'sprintf'
)
) {
$type = new UnionType([
new StringAlwaysAcceptingObjectWithToStringType(),
new IntegerType(),
new FloatType(),
new NullType(),
new BooleanType(),
]);
}

if (
$parameterSignature->getName() === 'fields'
&& $lowerCasedFunctionName === 'fputcsv'
) {
$type = new ArrayType(
new UnionType([
new StringType(),
new IntegerType(),
]),
new UnionType([
new StringAlwaysAcceptingObjectWithToStringType(),
new IntegerType(),
new FloatType(),
new NullType(),
new BooleanType(),
]),
);
}

return new NativeParameterWithPhpDocsReflection(
$parameterSignature->getName(),
Expand Down

0 comments on commit 670d04d

Please sign in to comment.