Skip to content

Commit 7d7441f

Browse files
committed
minor symfony#58094 Use Stringable whenever possible (fabpot)
This PR was merged into the 7.2 branch. Discussion ---------- Use Stringable whenever possible | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | n/a | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 06b4996 Use Stringable whenever possible
2 parents 8d96608 + 06b4996 commit 7d7441f

File tree

7 files changed

+8
-8
lines changed

7 files changed

+8
-8
lines changed

src/Symfony/Component/BrowserKit/HttpBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function getBodyAndExtraHeaders(Request $request, array $headers): array
9999
if ($vars = get_object_vars($v)) {
100100
array_walk_recursive($vars, $caster);
101101
$v = $vars;
102-
} elseif (method_exists($v, '__toString')) {
102+
} elseif ($v instanceof \Stringable) {
103103
$v = (string) $v;
104104
}
105105
}

src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private function checkType(Definition $checkedDefinition, mixed $value, \Reflect
274274
return;
275275
}
276276

277-
if ('string' === $type && method_exists($class, '__toString')) {
277+
if ('string' === $type && $class instanceof \Stringable) {
278278
return;
279279
}
280280

src/Symfony/Component/ErrorHandler/BufferingLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __destruct()
5050
foreach ($this->logs as [$level, $message, $context]) {
5151
if (str_contains($message, '{')) {
5252
foreach ($context as $key => $val) {
53-
if (null === $val || \is_scalar($val) || (\is_object($val) && \is_callable([$val, '__toString']))) {
53+
if (null === $val || \is_scalar($val) || $val instanceof \Stringable) {
5454
$message = str_replace("{{$key}}", $val, $message);
5555
} elseif ($val instanceof \DateTimeInterface) {
5656
$message = str_replace("{{$key}}", $val->format(\DateTimeInterface::RFC3339), $message);

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(mixed $data = null, int $status = 200, array $header
4040
{
4141
parent::__construct('', $status, $headers);
4242

43-
if ($json && !\is_string($data) && !is_numeric($data) && !\is_callable([$data, '__toString'])) {
43+
if ($json && !\is_string($data) && !is_numeric($data) && !$data instanceof \Stringable) {
4444
throw new \TypeError(\sprintf('"%s": If $json is set to true, argument $data must be a string or object implementing __toString(), "%s" given.', __METHOD__, get_debug_type($data)));
4545
}
4646

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
266266
if ($vars = get_object_vars($v)) {
267267
array_walk_recursive($vars, $caster);
268268
$v = $vars;
269-
} elseif (method_exists($v, '__toString')) {
269+
} elseif ($v instanceof \Stringable) {
270270
$v = (string) $v;
271271
}
272272
}

src/Symfony/Component/Security/Core/Authorization/TraceableAccessDecisionManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getStrategy(): string
8585
if (null === $this->strategy) {
8686
return '-';
8787
}
88-
if (method_exists($this->strategy, '__toString')) {
88+
if ($this->strategy instanceof \Stringable) {
8989
return (string) $this->strategy;
9090
}
9191

src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ private function getCredentials(Request $request): array
133133

134134
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $credentials['username']);
135135

136-
if (!\is_string($credentials['password']) && (!\is_object($credentials['password']) || !method_exists($credentials['password'], '__toString'))) {
136+
if (!\is_string($credentials['password']) && !$credentials['password'] instanceof \Stringable) {
137137
throw new BadRequestHttpException(\sprintf('The key "%s" must be a string, "%s" given.', $this->options['password_parameter'], \gettype($credentials['password'])));
138138
}
139139

140140
if ('' === (string) $credentials['password']) {
141141
throw new BadCredentialsException(\sprintf('The key "%s" must be a non-empty string.', $this->options['password_parameter']));
142142
}
143143

144-
if (!\is_string($credentials['csrf_token'] ?? '') && (!\is_object($credentials['csrf_token']) || !method_exists($credentials['csrf_token'], '__toString'))) {
144+
if (!\is_string($credentials['csrf_token'] ?? '') && !$credentials['csrf_token'] instanceof \Stringable) {
145145
throw new BadRequestHttpException(\sprintf('The key "%s" must be a string, "%s" given.', $this->options['csrf_parameter'], \gettype($credentials['csrf_token'])));
146146
}
147147

0 commit comments

Comments
 (0)