Skip to content

Commit

Permalink
minor #54688 [TypeInfo] Support \Stringable objects in `StringTypeR…
Browse files Browse the repository at this point in the history
…esolver` (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
  • Loading branch information
nicolas-grekas committed Apr 24, 2024
2 parents 02c1e3c + 3870406 commit 3903840
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public function testResolve(Type $expectedType, string $string, ?TypeContext $ty
$this->assertEquals($expectedType, $this->resolver->resolve($string, $typeContext));
}

/**
* @dataProvider resolveDataProvider
*/
public function testResolveStringable(Type $expectedType, string $string, ?TypeContext $typeContext = null)
{
$this->assertEquals($expectedType, $this->resolver->resolve(new class($string) implements \Stringable {
public function __construct(private string $value)
{
}

public function __toString(): string
{
return $this->value;
}
}, $typeContext));
}

/**
* @return iterable<array{0: Type, 1: string, 2?: TypeContext}>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function __construct()

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

Expand Down

0 comments on commit 3903840

Please sign in to comment.