1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Merge pull request #10231 from MidnightDesign/sprintf-stringable-values

Allow stringable objects in `sprintf()` values
This commit is contained in:
orklah 2023-09-29 11:38:37 +02:00 committed by GitHub
commit db07b05156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 14 deletions

View File

@ -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
*

View File

@ -125,6 +125,18 @@ class CoreStubsTest extends TestCase
'$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);

View File

@ -261,19 +261,6 @@ class ToStringTest extends TestCase
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 {