Skip to content

Commit 3870406

Browse files
committed
support Stringable objects in StringTypeResolver
1 parent 02c1e3c commit 3870406

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ public function testResolve(Type $expectedType, string $string, ?TypeContext $ty
3939
$this->assertEquals($expectedType, $this->resolver->resolve($string, $typeContext));
4040
}
4141

42+
/**
43+
* @dataProvider resolveDataProvider
44+
*/
45+
public function testResolveStringable(Type $expectedType, string $string, ?TypeContext $typeContext = null)
46+
{
47+
$this->assertEquals($expectedType, $this->resolver->resolve(new class($string) implements \Stringable {
48+
public function __construct(private string $value)
49+
{
50+
}
51+
52+
public function __toString(): string
53+
{
54+
return $this->value;
55+
}
56+
}, $typeContext));
57+
}
58+
4259
/**
4360
* @return iterable<array{0: Type, 1: string, 2?: TypeContext}>
4461
*/

src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public function __construct()
7171

7272
public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
7373
{
74-
if (!\is_string($subject)) {
74+
if ($subject instanceof \Stringable) {
75+
$subject = (string) $subject;
76+
} elseif (!\is_string($subject)) {
7577
throw new UnsupportedException(sprintf('Expected subject to be a "string", "%s" given.', get_debug_type($subject)), $subject);
7678
}
7779

0 commit comments

Comments
 (0)