Skip to content

Commit

Permalink
Merge pull request #10231 from MidnightDesign/sprintf-stringable-values
Browse files Browse the repository at this point in the history
Allow stringable objects in `sprintf()` values
  • Loading branch information
orklah committed Sep 29, 2023
2 parents 2d3a625 + c4c8ef5 commit db07b05
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ function preg_quote(string $str, ?string $delimiter = null) : string {}
/**
* @psalm-pure
*
* @param string|int|float $values
* @param string|stringable-object|int|float $values
* @return (PHP_MAJOR_VERSION is 8 ? string : string|false)
* @psalm-ignore-falsable-return
*
Expand Down
12 changes: 12 additions & 0 deletions tests/CoreStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ function foo(string $foo): string
'$a===' => 'string',
],
];
yield 'sprintf accepts Stringable values' => [
'code' => '<?php
$a = sprintf(
"%s",
new class implements Stringable { public function __toString(): string { return "hello"; } },
);
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
];
yield 'json_encode returns a non-empty-string provided JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE' => [
'code' => '<?php
$a = json_encode([], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
Expand Down
13 changes: 0 additions & 13 deletions tests/ToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,6 @@ function fooFoo(string $b): void {}
fooFoo(new A());',
'error_message' => 'InvalidArgument',
],
'implicitCastWithStrictTypesToEchoOrSprintf' => [
'code' => '<?php declare(strict_types=1);
class A {
public function __toString(): string
{
return "hello";
}
}
echo(new A());
sprintf("hello *", new A());',
'error_message' => 'ImplicitToStringCast',
],
'implicitCast' => [
'code' => '<?php
class A {
Expand Down

0 comments on commit db07b05

Please sign in to comment.