Skip to content

Commit

Permalink
use benevolent union
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 28, 2024
1 parent d6acb86 commit aed314b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<false|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 @@ -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<false|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: 2 additions & 1 deletion src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/sprintf-php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

0 comments on commit aed314b

Please sign in to comment.