Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow stringable objects in sprintf() values #10231

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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