Skip to content

Commit 3903840

Browse files
minor symfony#54688 [TypeInfo] Support \Stringable objects in StringTypeResolver (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [TypeInfo] Support `\Stringable` objects in `StringTypeResolver` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 3870406 support Stringable objects in StringTypeResolver
2 parents 02c1e3c + 3870406 commit 3903840

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)