From aed314b50049bae0e92d7947794d49f4a8e702c7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 28 Jun 2024 10:46:09 +0200 Subject: [PATCH] use benevolent union --- resources/functionMap.php | 4 ++-- .../Php/SprintfFunctionDynamicReturnTypeExtension.php | 3 ++- tests/PHPStan/Analyser/nsrt/sprintf-php7.php | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/resources/functionMap.php b/resources/functionMap.php index 8c88f3b06b..cf69e00e81 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -11689,7 +11689,7 @@ 'Spoofchecker::setAllowedLocales' => ['void', 'locale_list'=>'string'], 'Spoofchecker::setChecks' => ['void', 'checks'=>'long'], 'Spoofchecker::setRestrictionLevel' => ['void', 'restriction_level'=>'int'], -'sprintf' => ['false|string', 'format'=>'string', '...values='=>'__stringAndStringable|int|float|null|bool'], +'sprintf' => ['__benevolent', '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'], @@ -13112,7 +13112,7 @@ '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<__stringAndStringable|int|float|null|bool>'], -'vsprintf' => ['false|string', 'format'=>'string', 'args'=>'array<__stringAndStringable|int|float|null|bool>'], +'vsprintf' => ['__benevolent', '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='=>''], diff --git a/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php b/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php index e96fbe3527..0f72f9bc49 100644 --- a/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php @@ -12,6 +12,7 @@ use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; use PHPStan\Type\Accessory\AccessoryNumericStringType; +use PHPStan\Type\BenevolentUnionType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\DynamicFunctionReturnTypeExtension; use PHPStan\Type\IntegerRangeType; @@ -122,7 +123,7 @@ public function getTypeFromFunctionCall( } if (!$this->phpVersion->throwsTypeErrorForInternalFunctions()) { - $returnType = TypeCombinator::union($returnType, new ConstantBooleanType(false)); + $returnType = new BenevolentUnionType([$returnType, new ConstantBooleanType(false)]); } return $this->getConstantType($args, $returnType, $functionReflection, $scope); diff --git a/tests/PHPStan/Analyser/nsrt/sprintf-php7.php b/tests/PHPStan/Analyser/nsrt/sprintf-php7.php index 027b9608ca..03ddbe4541 100644 --- a/tests/PHPStan/Analyser/nsrt/sprintf-php7.php +++ b/tests/PHPStan/Analyser/nsrt/sprintf-php7.php @@ -6,9 +6,9 @@ function sprintfCanReturnFalse(string $format, array $arr): void { - assertType('string|false', sprintf($format, ...$arr)); - assertType('string|false', vsprintf($format, $arr)); + assertType('(string|false)', sprintf($format, ...$arr)); + assertType('(string|false)', vsprintf($format, $arr)); - assertType('non-falsy-string|false', sprintf("%s", ...$arr)); // should be 'string|false' - assertType('non-falsy-string|false', vsprintf("%s", $arr)); // should be 'string|false' + assertType('(non-falsy-string|false)', sprintf("%s", ...$arr)); // should be 'string|false' + assertType('(non-falsy-string|false)', vsprintf("%s", $arr)); // should be 'string|false' }